{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\User\Dropbox\04_SLERs\CODE\UOAContest\104Log20181024.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}24 Oct 2018, 02:33:38

{com}. do "C:\Users\User\AppData\Local\Temp\STD2a98_000000.tmp"
{txt}
{com}. *Code restructures SLERs so that the contest is the unit of analysis.  
. *Code also creates variables that are intended to predict Democratic vote share.  
. 
. *GOAL
. *Create a file for analyses with
. *-the contest as the unit of analysis
. *-dependent variable: the percent of the 2-party vote going to the Democrat
. 
. *THEME: DEATH BY A 1,000 CUTS.  
. 
. 
. clear
{txt}
{com}. version 15.1
{txt}
{com}. set varabbrev off, permanently
{txt}({cmd:set varabbrev} preference recorded)

{com}. cd C:\Users\User\Dropbox\04_SLERs\CODE\UOAContest
{res}C:\Users\User\Dropbox\04_SLERs\CODE\UOAContest
{txt}
{com}. 
. *Replace "20181024" with today's date.  
. global datezzz 20181024
{txt}
{com}. 
. *put name of main file to be created here
. global mainfile 102slersuoacontest$datezzz
{txt}
{com}. *put the name of the slers file used here
. global slersfile 001_196slers1967to2016_20180908
{txt}
{com}. 
. *CHAMBER SEATS
. *Create file with the number of seats up in each chamber and the proportion of seats up in each chamber.  
. *EXPLANATION: THESE CHAMBER-YEAR VARIABLES WILL BE USED TO 1) ASSESS WHETHER ALL SEATS ARE ACCOUNTED FOR AT THE END AND 2) AID FIGURING OUT WHETHER LAGGED VARIABLES NEED TO BE CHANGED TO SYSTEM MISSING BECAUSE OF REDISTRICTING.
. clear
{txt}
{com}. import excel 002From_StatePartisanBalance1777to2016_20171027_SourceFiles.xlsx, firstrow
{res}{txt}
{com}. rename electyear year
{res}{txt}
{com}. drop if year<1967
{txt}(19,098 observations deleted)

{com}. gen sen=chambercode==8
{txt}
{com}. rename stateno sid
{res}{txt}
{com}. keep year sid sen totinsess
{txt}
{com}. save tempchamberseats, replace
{txt}(note: file tempchamberseats.dta not found)
file tempchamberseats.dta saved

{com}. clear
{txt}
{com}. use $slersfile
{txt}
{com}. keep if deter==1&outcome=="w"
{txt}(202,079 observations deleted)

{com}. gen temp=mod(termz,1)
{txt}
{com}. replace year=year-1 if temp==.5
{txt}(3 real changes made)

{com}. *EXPLANATION: THE FOLLOWING LINE IS NECESSARY BECAUSE WE WANT TO COUNT SEATS.  BUT IF ONE WINNER HAS MULTIPLE LINES IN ONE CONTEST BECAUSE 1) THEY ARE RUNNING ON MULTIPLE PARTIES OR 2) COUNTY BREAKDOWNS ARE REPORTED, THEY HAVE TO BE REDUCED TO ONE LINE.  
. collapse (mean) eseats, by(year sid sen dname dno geopost mmdpost cand)
{txt}
{com}. gen c=1
{txt}
{com}. collapse (mean) eseats (sum) c, by(year sid sen dname dno geopost mmdpost)
{txt}
{com}. replace eseats=2 if sid==45&sen==0&year==1986&dname=="orleans"&dno==3
{txt}(1 real change made)

{com}. assert eseats==c
{txt}
{com}. drop c
{txt}
{com}. collapse (sum) seatsup=eseats, by(year sid sen)
{txt}
{com}. merge 1:1 year sid sen using tempchamberseats
{res}{txt}{p 0 7 2}
(note: variable
year was 
int, now double to accommodate using data's values)
{p_end}
{p 0 7 2}
(note: variable
sen was 
byte, now float to accommodate using data's values)
{p_end}

{col 5}Result{col 38}# of obs.
{col 5}{hline 41}
{col 5}not matched{col 30}{res}           2,708
{txt}{col 9}from master{col 30}{res}               0{txt}  (_merge==1)
{col 9}from using{col 30}{res}           2,708{txt}  (_merge==2)

{col 5}matched{col 30}{res}           2,294{txt}  (_merge==3)
{col 5}{hline 41}

{com}. drop if _merge==2
{txt}(2,708 observations deleted)

{com}. gen propup=seatsup/totinsess
{txt}
{com}. drop if propup==.
{txt}(0 observations deleted)

{com}. assert propup<=1
{txt}
{com}. *seatprop never more than 1, good
. assert sid==2&sen==1&year==2012 if propup>.9&propup!=1
{txt}
{com}. *that should be ak 2012 sen propup=.95, if its anything else or more, there could potentially be a mistake.  
. drop _merge
{txt}
{com}. rename totinsess totalseats
{res}{txt}
{com}. save tempchamberseats, replace
{txt}file tempchamberseats.dta saved

{com}. 
. *SLERs
. clear
{txt}
{com}. use $slersfile
{txt}
{com}. 
. *RUNOFFS
. *for the three runoff general elections in the dataset, change the winner of the runoff to the winner in the first round, and change deter=0, and changes eseats.  The first round can be used for vote share, but now the winner doesn't correspond necessarily to the highest vote getter.  But the ultimate winner in the runoff is preserved so that winners can be aggregated when appropriate.  
. *GA 1968 HS
. gen temp=sid==10&sen==0&dno==73&mmdpost==2&year==1968
{txt}
{com}. list etype deter cand outcome eseats dtype caseid if temp
{txt}
        {c TLC}{hline 12}{c -}{hline 7}{c -}{hline 24}{c -}{hline 9}{c -}{hline 8}{c -}{hline 7}{c -}{hline 8}{c TRC}
        {c |} {res}     etype   deter                     cand   outcome   eseats   dtype   caseid {txt}{c |}
        {c LT}{hline 12}{c -}{hline 7}{c -}{hline 24}{c -}{hline 9}{c -}{hline 8}{c -}{hline 7}{c -}{hline 8}{c RT}
 52614. {c |} {res}dpfsettled       0   setliff, carter andrew         w        1       2    46055 {txt}{c |}
 52615. {c |} {res}rpfsettled       0            edwards, jack         w        1       2    46051 {txt}{c |}
 52616. {c |} {res}rpfsettled       0      sechler, raymond g.         l        1       2    46054 {txt}{c |}
 52923. {c |} {res}   gfunset       0   setliff, carter andrew         l        1       2    46056 {txt}{c |}
 52924. {c |} {res}   gfunset       0         morris, larry c.         r        1       2    46053 {txt}{c |}
        {c LT}{hline 12}{c -}{hline 7}{c -}{hline 24}{c -}{hline 9}{c -}{hline 8}{c -}{hline 7}{c -}{hline 8}{c RT}
 52925. {c |} {res}   gfunset       0            edwards, jack         r        1       2    46052 {txt}{c |}
 53045. {c |} {res}   grunoff       1         morris, larry c.         w        1       2    45998 {txt}{c |}
 53046. {c |} {res}   grunoff       1            edwards, jack         l        1       2    45997 {txt}{c |}
        {c BLC}{hline 12}{c -}{hline 7}{c -}{hline 24}{c -}{hline 9}{c -}{hline 8}{c -}{hline 7}{c -}{hline 8}{c BRC}

{com}. replace outcome="w" if caseid==46053
{txt}(1 real change made)

{com}. replace outcome="l" if caseid==46052
{txt}(1 real change made)

{com}. replace eseats=1 if temp&etype=="gfunset"
{txt}(0 real changes made)

{com}. replace deter=1 if temp&etype=="gfunset"
{txt}(3 real changes made)

{com}. replace deter=0 if temp&etype=="grunoff"
{txt}(2 real changes made)

{com}. *GA 2010 HS
. replace temp=sid==10&sen==0&dno==29&year==2000
{txt}(13 real changes made)

{com}. list etype deter cand outcome eseats dtype caseid if temp
{txt}
        {c TLC}{hline 9}{c -}{hline 7}{c -}{hline 17}{c -}{hline 9}{c -}{hline 8}{c -}{hline 7}{c -}{hline 8}{c TRC}
        {c |} {res}  etype   deter              cand   outcome   eseats   dtype   caseid {txt}{c |}
        {c LT}{hline 9}{c -}{hline 7}{c -}{hline 17}{c -}{hline 9}{c -}{hline 8}{c -}{hline 7}{c -}{hline 8}{c RT}
 60789. {c |} {res}gfunset       0     sauder, randy         r        1       1    42772 {txt}{c |}
 60790. {c |} {res}gfunset       0   collins, ginger         r        1       1    42769 {txt}{c |}
 60791. {c |} {res}gfunset       0      stoner, doug         l        1       1    42773 {txt}{c |}
 60986. {c |} {res}grunoff       1     sauder, randy         l        1       1    42771 {txt}{c |}
 60987. {c |} {res}grunoff       1   collins, ginger         w        1       1    42770 {txt}{c |}
        {c BLC}{hline 9}{c -}{hline 7}{c -}{hline 17}{c -}{hline 9}{c -}{hline 8}{c -}{hline 7}{c -}{hline 8}{c BRC}

{com}. replace outcome="w" if caseid==42769
{txt}(1 real change made)

{com}. replace outcome="l" if caseid==42772
{txt}(1 real change made)

{com}. replace eseats=1 if temp&etype=="gfunset"
{txt}(0 real changes made)

{com}. replace deter=1 if temp&etype=="gfunset"
{txt}(3 real changes made)

{com}. replace deter=0 if temp&etype=="grunoff"
{txt}(2 real changes made)

{com}. *VT 1986 HS
. replace temp=sid==45&sen==0&dname=="orleans"&dno==3&year==1986
{txt}(12 real changes made)

{com}. list etype deter cand outcome eseats dtype caseid if temp
{txt}
        {c TLC}{hline 13}{c -}{hline 7}{c -}{hline 20}{c -}{hline 9}{c -}{hline 8}{c -}{hline 7}{c -}{hline 8}{c TRC}
        {c |} {res}      etype   deter                 cand   outcome   eseats   dtype   caseid {txt}{c |}
        {c LT}{hline 13}{c -}{hline 7}{c -}{hline 20}{c -}{hline 9}{c -}{hline 8}{c -}{hline 7}{c -}{hline 8}{c RT}
337484. {c |} {res}gfpartunset       1     blais, benoit u.         w        2       3   240176 {txt}{c |}
337485. {c |} {res}gfpartunset       1   fletcher, roger e.         r        2       3   240178 {txt}{c |}
337486. {c |} {res}gfpartunset       1         davis, ercil         l        2       3   240177 {txt}{c |}
337487. {c |} {res}gfpartunset       1      hannan, paul w.         r        2       3   240180 {txt}{c |}
337488. {c |} {res}gfpartunset       1           scattering         l        2       3   240182 {txt}{c |}
        {c LT}{hline 13}{c -}{hline 7}{c -}{hline 20}{c -}{hline 9}{c -}{hline 8}{c -}{hline 7}{c -}{hline 8}{c RT}
337620. {c |} {res}    grunoff       1   fletcher, roger e.         l        1       3   240179 {txt}{c |}
337621. {c |} {res}    grunoff       1      hannan, paul w.         w        1       3   240181 {txt}{c |}
        {c BLC}{hline 13}{c -}{hline 7}{c -}{hline 20}{c -}{hline 9}{c -}{hline 8}{c -}{hline 7}{c -}{hline 8}{c BRC}

{com}. replace outcome="w" if caseid==240180
{txt}(1 real change made)

{com}. replace outcome="l" if caseid==240178
{txt}(1 real change made)

{com}. replace eseats=2 if temp&etype=="gfpartunset"
{txt}(0 real changes made)

{com}. replace deter=1 if temp&etype=="gfpartunset"
{txt}(0 real changes made)

{com}. replace deter=0 if temp&etype=="grunoff"
{txt}(2 real changes made)

{com}. drop temp
{txt}
{com}. 
. *CASE SELECTION
. *I MOVED THIS SECTION SINCE THE VIDEO OF OCT 21, 2018.
. *FL 2014 HS
. *The outcome of the following election was thrown out.  But the person who received the most votes in the general election in question also won the special election that was called later to fill the seat, so the special election can be changed to deter=1 and the initial election can be kept.  
. drop if sid==9&sen==0&dno==64&year==2015
{txt}(2 observations deleted)

{com}. replace deter=1 if sid==9&sen==0&dno==64&year==2014&etype=="g"
{txt}(4 real changes made)

{com}. *etype=ssg are held at irregular times, but should be kept for lagging vote share.  
. keep if deter==1|etype=="g"|etype=="gs"|etype=="ssg"
{txt}(53,369 observations deleted)

{com}. *drop cases that are in non-partisan election state-years.  
. drop if sid==27|(sid==23&year<1974)
{txt}(2,466 observations deleted)

{com}. *Drop cases that are in Louisiana
. drop if sid==18
{txt}(3,610 observations deleted)

{com}. 
. *DONTUSE
. *dontuse=1 means that the election shouldn't be used for an analysis of the determinants of vote share, although it might be appropriate to use for other purposes, such as tabulating winners for a party by chamber, etc.  
. *The following identifies cases that are missing important variables as cases not to use.
. *I WENT THROUGH EVERY ELECTION WITH THESE AND PREVENTED A BUNCH OF ELECTIONS FROM BEING UNNECESSARILY EXCLUDED SINCE THE VIDEO I MADE ON OCT 21, 2018.  A LOT OF THE UNCERT=identity ENTRIES LOOK LIKE THINGS I'VE CORRECTED SINCE THEN, BUT THAT UNCERT MAY NOT HAVE BEEN CHANGED TO SYSMIS.  I WILL DEAL WITH THIS WITH THE NEXT EDITION AFTER THE 2018 ELECTION.  
. *Unlike the code I went through in the video, this doesn't make cases dontuse=1 if uncert=vote.  Such cases will be excluded later, but not if they're in uncontested elections, even if they are in a state that doesn't leave uncontested elections off the ballot.  
. *I also noticed that uncert="truepartysource" cases were being excluded because "party" appears in that string, and these shouldn't be excluded.  
. *The variable "uncert" has a leading space in it, and so is corrected here.
. replace uncert=stritrim(strtrim(lower(uncert)))
{txt}(74 real changes made)

{com}. *I also looked through all the names that have uncert="identity".  I believe many of those are cases I corrected, or are probably correct as they are, so I'm not excluding them either for this run.  
. *Excluding too many cases is especially a problem for people who want to do analyses at the district level and then say things about chambers, such as analyses of redistricting plans.  
. *DONTUSE
. gen dontuse=etype!="g"&etype!="gs"&etype!="ssg"
{txt}
{com}. foreach string in dno party eseats etype outcome writeinstatus generalproblem day incompleteelect {c -(}
{txt}  2{com}. gen temp=regexm(uncert,"`string'")
{txt}  3{com}. replace dontuse=1 if temp==1&uncert!="truepartysource"
{txt}  4{com}. drop temp
{txt}  5{com}. {c )-}
{txt}(2 real changes made)
(505 real changes made)
(0 real changes made)
(3 real changes made)
(3 real changes made)
(0 real changes made)
(1 real change made)
(0 real changes made)
(176 real changes made)

{com}. 
. 
. 
. *OVERVIEW: HOW NON-MAJOR PARTY CANDIDATES & VOTES DELT WITH.  
. *A variable "bigthird" will track whether contests should be excluded because of a large third party presence.  
. *If more than 20% of total votes are for non-major party candidates, the contest is excluded from analysis.  (Non-major party incumbents or other prior legislators also result in exclusion.)
. *(An alternative approach would be to model the impact of varying percents of non-major party votes on vote share.)
. *However, before computing this amount, we should exclude small write-in candidates from the denominator of the above fraction.
. *This should be done because states and years vary greatly in whether write-in / scattering figures are reported, and whether they were collected.  Excluding them makes the comparable over time.  
. *Write-in candidates who were incorrectly coded as Democrats or Republicans should also be identified and dropped.  
. 
. *WRITEIN
. *The following has to be done before the collapse.  
. gen writein=1 if caseid==142300
{txt}(318,897 missing values generated)

{com}. replace writein=1 if caseid==142487
{txt}(1 real change made)

{com}. egen max=max(writein), by(year sid sen dname dno geopost mmdpost etype)
{txt}(318892 missing values generated)

{com}. list year sid sen dname dno geopost mmdpost etype party partyz partyt cand vote if max==1
{txt}
        {c TLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 18}{c -}{hline 8}{c -}{hline 8}{c -}{hline 24}{c -}{hline 7}{c TRC}
        {c |} {res}year   sid   sen   dname   dno   geopost   mmdpost   etype              party   partyz   partyt                     cand    vote {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 18}{c -}{hline 8}{c -}{hline 8}{c -}{hline 24}{c -}{hline 7}{c RT}
174024. {c |} {res}2000    29     1            12         .         .       g           democrat        d        d      franks, suzan l. r.   10305 {txt}{c |}
174025. {c |} {res}2000    29     1            12         .         .       g        independent   nonmaj   nonmaj           squires, james      48 {txt}{c |}
174026. {c |} {res}2000    29     1            12         .         .       g   modernrepublican        r        r             ohearn, jane   12612 {txt}{c |}
174039. {c |} {res}2000    29     1            17         .         .       g           democrat        d        d   devine, james e. (jim)    8216 {txt}{c |}
174040. {c |} {res}2000    29     1            17         .         .       g        independent   nonmaj   nonmaj              brown, mary     131 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 18}{c -}{hline 8}{c -}{hline 8}{c -}{hline 24}{c -}{hline 7}{c RT}
174041. {c |} {res}2000    29     1            17         .         .       g   modernrepublican        r        r      barnes, john s. jr.   13988 {txt}{c |}
        {c BLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 18}{c -}{hline 8}{c -}{hline 8}{c -}{hline 24}{c -}{hline 7}{c BRC}

{com}. *I'm not sure why I concluded in the past these were writeins and not filed independents.  Make them writeins.  
. replace party="writein" if writein==1
{txt}(2 real changes made)

{com}. replace partyz="writein" if writein==1
{txt}(2 real changes made)

{com}. replace partyt="writein" if writein==1
{txt}(2 real changes made)

{com}. 
. *Verify that partyt doesn't vary within a candidate-election.
. bysort year sid sen dname dno geopost mmdpost etype candid: gen sum1=_N
{txt}
{com}. bysort year sid sen dname dno geopost mmdpost etype candid partyt: gen sum2=_N
{txt}
{com}. assert sum1==sum2
{txt}
{com}. 
. *UOA CAND-PARTY
. *Collapse data so that county breakdowns are no longer present.  This will make it easier to see if candidates are running on multiple lines.  
. gen votemiss=vote==.
{txt}
{com}. collapse (mean) votemiss (sum) vote (max) dontuse firstcase, by(year sid sen dname dno geopost mmdpost specpost cand candid termz outcome exper tenure1 tenure2 deter etype eseats dseats dtype popnum regime redist redist1 redist2 redist3 nest nest1 nest2 nest3 party partyz partyt)
{txt}
{com}. 
. *VOTEMISS
. *verify that votemiss is either 0 or 1, and not in between.
. assert votemiss==0|votemiss==1
{txt}
{com}. *that is correct
. assert vote==0 if votemiss==1
{txt}
{com}. *replace vote=0 with system missing to deal with Stata's problem with this.  
. replace vote=. if votemiss==1
{txt}(5,649 real changes made, 5,649 to missing)

{com}. 
. *WRITEINS
. *Get rid of scattering of all vote amounts as well as write-ins identified by name who receive less than 5% of the vote
. drop if cand=="scattering"|cand=="writein"
{txt}(12,407 observations deleted)

{com}. 
. *NONMAJ
. *Drop non-major party candidates (including writeins) if all such candidates in one election received 5% or less of the total vote.  Doing this won't influence how the variable "bigthird" is coded, it will merely make it faster to assess who is a write-in and who isn't.  
. bysort year sid sen dname dno geopost mmdpost etype candid: gen rows=_N
{txt}
{com}. gen tempvote=vote if partyt!="d"&partyt!="r"
{txt}(267,041 missing values generated)

{com}. egen sum1=sum(tempvote), by(year sid sen dname dno geopost mmdpost etype)
{txt}
{com}. egen sum2=sum(vote), by(year sid sen dname dno geopost mmdpost etype)
{txt}
{com}. *Contests with more than 1 seat should have the percentage adjusted upwards.  That means that if there is (say) a three seat contest, non-major party candidates will only be dropped if they collectively have 1.67% or less of the total vote.  This is because they are more likely to influence the fortunes of whether specific Democratic or Republican candidates win, per percentage point of the total, if there are more seats to win.  
. gen nonmajvote=((sum1*eseats)/(sum1+sum2))*100
{txt}(5,644 missing values generated)

{com}. drop if partyt!="d"&partyt!="r"&nonmajvote<5&rows==1
{txt}(11,182 observations deleted)

{com}. drop sum1 sum2 nonmajvote
{txt}
{com}. 
. *WRITEINS
. *Clear up problems associated with probable writeins who weren't coded as such in SLERs (I will deal with these better in the future).  First I identify those getting fewer than 10 votes, than those getting between 11 and 20 votes just to see how many there are of each.  Not all candidates with those codes for v20 are write-ins, the number of votes they get is part of the evidence that they’re a write-in. 
. *Are there any writeins who are a separate line for a fused candidate, and so the writein status in question can be ignored?
. tab partyz partyt

 {txt}party for {c |}                       true party
       use {c |}         d     nonmaj  partymiss          r    writein {c |}     Total
{hline 11}{c +}{hline 55}{c +}{hline 10}
         b {c |}{res}       453          0          0        792          0 {txt}{c |}{res}     1,245 
{txt}         d {c |}{res}   135,270          0          0        143          0 {txt}{c |}{res}   135,413 
{txt}    nonmaj {c |}{res}     4,141     11,683          0      5,092          0 {txt}{c |}{res}    20,916 
{txt} partymiss {c |}{res}         0          0        249          0          0 {txt}{c |}{res}       249 
{txt}         r {c |}{res}       136          0          0    120,965          0 {txt}{c |}{res}   121,101 
{txt}   writein {c |}{res}        20          1          0         24        402 {txt}{c |}{res}       447 
{txt}{hline 11}{c +}{hline 55}{c +}{hline 10}
     Total {c |}{res}   140,020     11,684        249    127,016        402 {txt}{c |}{res}   279,371 
{txt}
{com}. *Yes, when partyz=writein, partyt=d 20 times, nonmaj 1 time, and r 24 times.  But make sure those are separate lines within one election.
. tab partyt if rows!=1

 {txt}true party {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          d {c |}{res}      7,888       46.07       46.07
{txt}     nonmaj {c |}{res}        209        1.22       47.29
{txt}          r {c |}{res}      9,013       52.64       99.92
{txt}    writein {c |}{res}         13        0.08      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}     17,123      100.00
{txt}
{com}. *There are 13 writeins, that is potentially a problem.  
. gen temp=partyt=="writein"&rows!=1
{txt}
{com}. egen max=max(temp), by(year sid sen dname dno geopost mmdpost etype candid)
{txt}
{com}. list year sid sen dname dno geopost mmdpost etype cand candid party partyz partyt vote if max==1
{txt}
        {c TLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 11}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 21}{c -}{hline 8}{c -}{hline 19}{c -}{hline 9}{c -}{hline 9}{c -}{hline 6}{c TRC}
        {c |} {res}year   sid   sen       dname   dno   geopost   mmdpost   etype                  cand   candid               party    partyz    partyt   vote {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 11}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 21}{c -}{hline 8}{c -}{hline 19}{c -}{hline 9}{c -}{hline 9}{c -}{hline 6}{c RT}
 16052. {c |} {res}1970    29     0     grafton     6         .         .       g   stoddard, lorree c.   151585               60328   writein   writein      1 {txt}{c |}
 16053. {c |} {res}1970    29     0     grafton     6         .         .       g   stoddard, lorree c.   151585               99991   writein   writein      1 {txt}{c |}
 16054. {c |} {res}1970    29     0     grafton     6         .         .       g      waterman, donald   151587               99991   writein   writein      1 {txt}{c |}
 16055. {c |} {res}1970    29     0     grafton     6         .         .       g      waterman, donald   151587               99992   writein   writein      2 {txt}{c |}
 16324. {c |} {res}1970    29     0   merrimack    19         .         .       g   thompson, arthur e.   145084               62838   writein   writein      1 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 11}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 21}{c -}{hline 8}{c -}{hline 19}{c -}{hline 9}{c -}{hline 9}{c -}{hline 6}{c RT}
 16325. {c |} {res}1970    29     0   merrimack    19         .         .       g   thompson, arthur e.   145084               60310   writein   writein      1 {txt}{c |}
 16479. {c |} {res}1970    29     0   strafford    10         .         .       g         rolfe, ernest   147556               60100   writein   writein     10 {txt}{c |}
 16480. {c |} {res}1970    29     0   strafford    10         .         .       g         rolfe, ernest   147556               60310   writein   writein      1 {txt}{c |}
 62988. {c |} {res}1978    34     1                33         .         .       g      jaeger, clarence   137214               81021   writein   writein     22 {txt}{c |}
 62989. {c |} {res}1978    34     1                33         .         .       g      jaeger, clarence   137214               81031   writein   writein     74 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 11}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 21}{c -}{hline 8}{c -}{hline 19}{c -}{hline 9}{c -}{hline 9}{c -}{hline 6}{c RT}
 62990. {c |} {res}1978    34     1                33         .         .       g      jaeger, clarence   137214   republicanwritein   writein   writein    312 {txt}{c |}
 88454. {c |} {res}1983     1     1                 8         .         .     ssg        barron, lowell     2369               99996   writein   writein      1 {txt}{c |}
 88455. {c |} {res}1983     1     1                 8         .         .     ssg        barron, lowell     2369   democraticwritein   writein   writein   7496 {txt}{c |}
        {c BLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 11}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 21}{c -}{hline 8}{c -}{hline 19}{c -}{hline 9}{c -}{hline 9}{c -}{hline 6}{c BRC}

{com}. *Those are legit, except for perhaps one (writein dem who received 7496).  One other was a writein repub who received 312, but that's not much.  Why does the number of votes matter?  These aren't problematic at all, they're all write-ins, even if there are multiple lines.  
. drop temp max
{txt}
{com}. *How many republicanwritein and democraticwritein cands are there?
. tab vote party if party=="republicanwritein"|party=="democraticwritein"

           {txt}{c |}    detailed party
(sum) vote {c |} democra..  republi.. {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
         6 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}        11 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}        13 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}        43 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}        48 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}       103 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}       154 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}       234 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}       237 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}       268 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}       290 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}       305 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}       312 {c |}{res}         1          1 {txt}{c |}{res}         2 
{txt}       409 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}       427 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}       568 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}       763 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}       784 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}       832 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}       842 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}       889 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}       982 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1038 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}      1119 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1205 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}      1254 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1472 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}      1503 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}      2051 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}      2499 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      2814 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      4254 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      4350 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      4355 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      4836 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}      5251 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      5719 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}      6119 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      7206 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}      7496 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      8469 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}     23011 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}     34860 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}        25         19 {txt}{c |}{res}        44 
{txt}
{com}. *17 repub, 23 dem, and some get very large vote numbers, 35k and 23k being the two highest.  
. list year sid sen dno party vote if party=="republicanwritein"|party=="democraticwritein"
{txt}
        {c TLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 19}{c -}{hline 7}{c TRC}
        {c |} {res}year   sid   sen   dno               party    vote {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 19}{c -}{hline 7}{c RT}
 10752. {c |} {res}1970     2     0    13   republicanwritein     305 {txt}{c |}
 10753. {c |} {res}1970     2     0    13   democraticwritein     409 {txt}{c |}
 10755. {c |} {res}1970     2     0    14   democraticwritein     842 {txt}{c |}
 11373. {c |} {res}1970     6     0    26   democraticwritein    4350 {txt}{c |}
 20566. {c |} {res}1970    47     1    36   republicanwritein    1503 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 19}{c -}{hline 7}{c RT}
 22918. {c |} {res}1972     6     0    60   democraticwritein    5251 {txt}{c |}
 34231. {c |} {res}1974     6     1    33   republicanwritein    5719 {txt}{c |}
 37000. {c |} {res}1974    20     0    23   democraticwritein     982 {txt}{c |}
 37150. {c |} {res}1974    20     1    25   democraticwritein     312 {txt}{c |}
 37182. {c |} {res}1974    20     1    45   democraticwritein    1254 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 19}{c -}{hline 7}{c RT}
 41523. {c |} {res}1974    38     0    46   republicanwritein    7206 {txt}{c |}
 43478. {c |} {res}1974    48     0    15   democraticwritein    4355 {txt}{c |}
 45309. {c |} {res}1976     5     0    74   republicanwritein   34860 {txt}{c |}
 55035. {c |} {res}1976    50     0     .   democraticwritein    1119 {txt}{c |}
 62990. {c |} {res}1978    34     1    33   republicanwritein     312 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 19}{c -}{hline 7}{c RT}
 70439. {c |} {res}1980    21     0     6   democraticwritein    6119 {txt}{c |}
 71521. {c |} {res}1980    26     0    82   democraticwritein     889 {txt}{c |}
 88455. {c |} {res}1983     1     1     8   democraticwritein    7496 {txt}{c |}
 92232. {c |} {res}1984    16     1    29   democraticwritein     784 {txt}{c |}
 92964. {c |} {res}1984    21     1     2   democraticwritein   23011 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 19}{c -}{hline 7}{c RT}
104879. {c |} {res}1986    29     0     2   democraticwritein     427 {txt}{c |}
104885. {c |} {res}1986    29     0     3   republicanwritein     237 {txt}{c |}
105281. {c |} {res}1986    29     0    19   democraticwritein     103 {txt}{c |}
105397. {c |} {res}1986    29     0     4   republicanwritein      43 {txt}{c |}
105489. {c |} {res}1986    31     0    22   republicanwritein     832 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 19}{c -}{hline 7}{c RT}
109589. {c |} {res}1986    48     1    12   republicanwritein    1205 {txt}{c |}
116260. {c |} {res}1988    31     1    19   republicanwritein    2051 {txt}{c |}
129689. {c |} {res}1990    39     0    69   democraticwritein     268 {txt}{c |}
129690. {c |} {res}1990    39     0    69   democraticwritein     290 {txt}{c |}
129691. {c |} {res}1990    39     0    69   democraticwritein     154 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 19}{c -}{hline 7}{c RT}
138463. {c |} {res}1992    29     0     7   republicanwritein    1038 {txt}{c |}
159370. {c |} {res}1996    16     1    15   republicanwritein    4836 {txt}{c |}
193708. {c |} {res}2002    20     0     4   republicanwritein    1472 {txt}{c |}
205517. {c |} {res}2004    21     0    34   democraticwritein    4254 {txt}{c |}
238694. {c |} {res}2010    20     1     2   republicanwritein    8469 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 19}{c -}{hline 7}{c RT}
246712. {c |} {res}2012     3     0    16   democraticwritein     234 {txt}{c |}
249197. {c |} {res}2012    14     0    56   republicanwritein      11 {txt}{c |}
250074. {c |} {res}2012    19     0    34   democraticwritein     568 {txt}{c |}
250330. {c |} {res}2012    19     1     8   republicanwritein      48 {txt}{c |}
256931. {c |} {res}2012    49     0     7   democraticwritein    2499 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 19}{c -}{hline 7}{c RT}
256946. {c |} {res}2012    49     0    17   republicanwritein       6 {txt}{c |}
257009. {c |} {res}2012    49     0    48   republicanwritein      13 {txt}{c |}
261523. {c |} {res}2014    20     0    40   democraticwritein    2814 {txt}{c |}
263886. {c |} {res}2014    31     0     5   democraticwritein     763 {txt}{c |}
        {c BLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 19}{c -}{hline 7}{c BRC}

{com}. *Re-verify that partyt doesn't vary within a candidate-election.
. bysort year sid sen dname dno geopost mmdpost etype candid: gen sum1=_N
{txt}
{com}. bysort year sid sen dname dno geopost mmdpost etype candid partyt: gen sum2=_N
{txt}
{com}. assert sum1==sum2
{txt}
{com}. *No problem.  
. drop sum1 sum2
{txt}
{com}. *Those are all d or r.  With the above established, partyz can be ignored.  
. gen writein=partyt=="writein"
{txt}
{com}. replace writein=1 if party=="99993"&vote<10&rows==1
{txt}(0 real changes made)

{com}. replace writein=1 if party=="99994"&vote<10&rows==1
{txt}(0 real changes made)

{com}. replace writein=1 if party=="99997"&vote<10&rows==1
{txt}(1 real change made)

{com}. replace writein=1 if party=="99998"&vote<10&rows==1
{txt}(0 real changes made)

{com}. replace writein=1 if party=="99993"&vote<20&rows==1
{txt}(0 real changes made)

{com}. replace writein=1 if party=="99994"&vote<20&rows==1
{txt}(0 real changes made)

{com}. replace writein=1 if party=="99997"&vote<20&rows==1
{txt}(0 real changes made)

{com}. replace writein=1 if party=="99998"&vote<20&rows==1
{txt}(0 real changes made)

{com}. egen sum1=sum(vote), by(year sid sen dname dno geopost mmdpost etype)
{txt}
{com}. egen sum2=sum(vote), by(year sid sen dname dno geopost mmdpost etype candid)
{txt}
{com}. *Adjust the amount upwards if there is more than one seat in the contest.  
. gen tempvoteper=((sum2*eseats)/sum1)*100
{txt}(5,644 missing values generated)

{com}. drop sum1 sum2
{txt}
{com}. replace writein=1 if party=="99993"&tempvoteper<2&rows==1
{txt}(0 real changes made)

{com}. replace writein=1 if party=="99994"&tempvoteper<2&rows==1
{txt}(1 real change made)

{com}. replace writein=1 if party=="99997"&tempvoteper<2&rows==1
{txt}(21 real changes made)

{com}. replace writein=1 if party=="99998"&tempvoteper<2&rows==1
{txt}(1 real change made)

{com}. replace writein=1 if tempvoteper<1&vote<50
{txt}(25 real changes made)

{com}. replace partyt="writein" if writein==1&rows==1
{txt}(49 real changes made)

{com}. *Re-verify that partyt doesn't vary within a candidate-election.
. bysort year sid sen dname dno geopost mmdpost etype candid: gen sum1=_N
{txt}
{com}. bysort year sid sen dname dno geopost mmdpost etype candid partyt: gen sum2=_N
{txt}
{com}. assert sum1==sum2
{txt}
{com}. *No problem.  
. drop sum1 sum2
{txt}
{com}. 
. *The following drops write-in candidates if the same individual is running in another district contemporaneously.  People often write in a candidate's name in a neighboring district, especially after redistricting where they write in an an incumbent they would like to still be able to vote for.  
. egen min=min(writein), by(year candid)
{txt}
{com}. tab year sid if writein==1&min==0&rows==1

           {txt}{c |}        number of state in alpha list
      year {c |}         1         12         29         39 {c |}     Total
{hline 11}{c +}{hline 44}{c +}{hline 10}
      1972 {c |}{res}         0          0          1          0 {txt}{c |}{res}         1 
{txt}      1978 {c |}{res}         0          1          0          0 {txt}{c |}{res}         1 
{txt}      1983 {c |}{res}         1          0          0          0 {txt}{c |}{res}         1 
{txt}      2002 {c |}{res}         0          0          0          8 {txt}{c |}{res}         8 
{txt}{hline 11}{c +}{hline 44}{c +}{hline 10}
     Total {c |}{res}         1          1          1          8 {txt}{c |}{res}        11 
{txt}
{com}. *There are only 11, 8 in RI 2002.  This is probably a mistake in the returns, which I'd guess I already looked into.  How many votes do they have?
. tab vote if writein==1&min==0&rows==1

 {txt}(sum) vote {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          1 {c |}{res}          3       27.27       27.27
{txt}          7 {c |}{res}          1        9.09       36.36
{txt}         25 {c |}{res}          1        9.09       45.45
{txt}         29 {c |}{res}          1        9.09       54.55
{txt}         39 {c |}{res}          1        9.09       63.64
{txt}        289 {c |}{res}          1        9.09       72.73
{txt}        331 {c |}{res}          1        9.09       81.82
{txt}        576 {c |}{res}          1        9.09       90.91
{txt}        841 {c |}{res}          1        9.09      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}         11      100.00
{txt}
{com}. *Most are extremely small amounts, but there are two that have more than 500, and two with between 200 and 500.  
. gen temp=1 if writein==1&min==0&rows==1&vote>200&vote!=.
{txt}(279,367 missing values generated)

{com}. egen max=max(temp), by(year sid sen dname dno geopost mmdpost etype)
{txt}(279355 missing values generated)

{com}. *dname, geopost or mmdpost are never obs in the following.  
. list year sid sen dno etype cand partyt vote temp if max==1
{txt}
        {c TLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 21}{c -}{hline 9}{c -}{hline 6}{c -}{hline 6}{c TRC}
        {c |} {res}year   sid   sen   dno   etype                  cand    partyt   vote   temp {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 21}{c -}{hline 9}{c -}{hline 6}{c -}{hline 6}{c RT}
198997. {c |} {res}2002    39     1     2       g      caprio, frank t.   writein    331      1 {txt}{c |}
198998. {c |} {res}2002    39     1     2       g          chancy, yvon         r    265      . {txt}{c |}
198999. {c |} {res}2002    39     1     2       g     espinal, pedro j.    nonmaj    342      . {txt}{c |}
199000. {c |} {res}2002    39     1     2       g   lee, rochelle bates    nonmaj    614      . {txt}{c |}
199001. {c |} {res}2002    39     1     2       g     pichardo, juan m.         d   3518      . {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 21}{c -}{hline 9}{c -}{hline 6}{c -}{hline 6}{c RT}
199002. {c |} {res}2002    39     1     2       g           toste, jeff   writein     29      . {txt}{c |}
199006. {c |} {res}2002    39     1     5       g      caprio, frank t.         d   3019      . {txt}{c |}
199007. {c |} {res}2002    39     1     5       g          chancy, yvon   writein     39      . {txt}{c |}
199008. {c |} {res}2002    39     1     5       g     espinal, pedro j.   writein      7      . {txt}{c |}
199009. {c |} {res}2002    39     1     5       g   lee, rochelle bates   writein     25      . {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 21}{c -}{hline 9}{c -}{hline 6}{c -}{hline 6}{c RT}
199010. {c |} {res}2002    39     1     5       g     pichardo, juan m.   writein    289      1 {txt}{c |}
199011. {c |} {res}2002    39     1     5       g           toste, jeff    nonmaj    879      . {txt}{c |}
199055. {c |} {res}2002    39     1    33       g     alves, stephen d.   writein    841      1 {txt}{c |}
199056. {c |} {res}2002    39     1    33       g    raptakis, leonidas         d   5639      . {txt}{c |}
199057. {c |} {res}2002    39     1    33       g   farnum, jonathan k.         r   3705      . {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 21}{c -}{hline 9}{c -}{hline 6}{c -}{hline 6}{c RT}
199058. {c |} {res}2002    39     1    33       g   clarke, john j. jr.   writein    576      1 {txt}{c |}
        {c BLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 21}{c -}{hline 9}{c -}{hline 6}{c -}{hline 6}{c BRC}

{com}. *compare them to themselves.  Do they have the same number of votes?
. gen temp2=1 if writein==1&min==0&rows==1
{txt}(279,360 missing values generated)

{com}. egen max2=max(temp2), by(year candid)
{txt}(279348 missing values generated)

{com}. sort candid year sen dno
{txt}
{com}. list year sid sen dno etype cand partyt vote temp2 if max2==1
{txt}
        {c TLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 21}{c -}{hline 9}{c -}{hline 6}{c -}{hline 7}{c TRC}
        {c |} {res}year   sid   sen   dno   etype                  cand    partyt   vote   temp2 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 21}{c -}{hline 9}{c -}{hline 6}{c -}{hline 7}{c RT}
  2844. {c |} {res}1983     1     0    23     ssg       richardson, ben         d   2092       . {txt}{c |}
  2845. {c |} {res}1983     1     1     8     ssg       richardson, ben   writein      1       1 {txt}{c |}
 48084. {c |} {res}1978    12     0    23       g        huyser, harold    nonmaj   3302       . {txt}{c |}
 48085. {c |} {res}1978    12     0    23       g        huyser, harold   writein      1       1 {txt}{c |}
119366. {c |} {res}1972    29     0    32       g        bossie, robert   writein      1       1 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 21}{c -}{hline 9}{c -}{hline 6}{c -}{hline 7}{c RT}
119367. {c |} {res}1972    29     1    20       g        bossie, robert         d   3810       . {txt}{c |}
119368. {c |} {res}1972    29     1    20       g        bossie, robert         d   6860       . {txt}{c |}
178376. {c |} {res}2002    39     1    32       g     alves, stephen d.         d   4160       . {txt}{c |}
178377. {c |} {res}2002    39     1    33       g     alves, stephen d.   writein    841       1 {txt}{c |}
178442. {c |} {res}2002    39     1     2       g      caprio, frank t.   writein    331       1 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 21}{c -}{hline 9}{c -}{hline 6}{c -}{hline 7}{c RT}
178443. {c |} {res}2002    39     1     5       g      caprio, frank t.         d   3019       . {txt}{c |}
178445. {c |} {res}2002    39     1     2       g          chancy, yvon         r    265       . {txt}{c |}
178446. {c |} {res}2002    39     1     5       g          chancy, yvon   writein     39       1 {txt}{c |}
178447. {c |} {res}2002    39     1     2       g     espinal, pedro j.    nonmaj    342       . {txt}{c |}
178448. {c |} {res}2002    39     1     5       g     espinal, pedro j.   writein      7       1 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 21}{c -}{hline 9}{c -}{hline 6}{c -}{hline 7}{c RT}
178449. {c |} {res}2002    39     1     2       g   lee, rochelle bates    nonmaj    614       . {txt}{c |}
178450. {c |} {res}2002    39     1     5       g   lee, rochelle bates   writein     25       1 {txt}{c |}
178451. {c |} {res}2002    39     1     2       g     pichardo, juan m.         d   3518       . {txt}{c |}
178452. {c |} {res}2002    39     1     5       g     pichardo, juan m.   writein    289       1 {txt}{c |}
178459. {c |} {res}2002    39     1     2       g           toste, jeff   writein     29       1 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 21}{c -}{hline 9}{c -}{hline 6}{c -}{hline 7}{c RT}
178460. {c |} {res}2002    39     1     5       g           toste, jeff    nonmaj    879       . {txt}{c |}
179084. {c |} {res}2002    39     1    32       g   clarke, john j. jr.         r   2391       . {txt}{c |}
179085. {c |} {res}2002    39     1    33       g   clarke, john j. jr.   writein    576       1 {txt}{c |}
        {c BLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 21}{c -}{hline 9}{c -}{hline 6}{c -}{hline 7}{c BRC}

{com}. *Many of those are clearly writeins.  I'm just going to drop them.  
. drop if writein==1&min==0&rows==1
{txt}(11 observations deleted)

{com}. *11 obs dropped, good.
. drop rows tempvote writein tempvoteper min temp max temp2 max2
{txt}
{com}. *The following drops write-in candidates who received less than 5% of the total vote (as recomputed).  The assumption here is that they are inconsistently reported across states, so if they are kept, states that are less apt to report writeins will have contests that are less likely to be coded as having a large third party.  To equalize exclusion rules across states, write-ins that receive very few votes are excluded from all states.  
. egen sum1=sum(vote), by(year sid sen dname dno geopost mmdpost etype)
{txt}
{com}. egen sum2=sum(vote), by(year sid sen dname dno geopost mmdpost etype candid)
{txt}
{com}. *Adjust the amount upwards if there is more than one seat in the contest.  
. gen tempvoteper=((sum2*eseats)/sum1)*100
{txt}(5,644 missing values generated)

{com}. drop sum1 sum2
{txt}
{com}. drop if partyt=="writein"&tempvoteper<5
{txt}(244 observations deleted)

{com}. *That deleted 244 more cases
. tab partyt

 {txt}true party {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          d {c |}{res}    140,017       50.16       50.16
{txt}     nonmaj {c |}{res}     11,644        4.17       54.34
{txt}  partymiss {c |}{res}        249        0.09       54.43
{txt}          r {c |}{res}    127,010       45.50       99.93
{txt}    writein {c |}{res}        196        0.07      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}    279,116      100.00
{txt}
{com}. *196 writeins left
. tab tempvoteper if partyt=="writein"

{txt}tempvoteper {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
   5.136268 {c |}{res}          1        0.51        0.51
{txt}   5.234796 {c |}{res}          1        0.51        1.02
{txt}   5.304237 {c |}{res}          1        0.51        1.53
{txt}   5.453155 {c |}{res}          1        0.51        2.04
{txt}   5.534591 {c |}{res}          1        0.51        2.55
{txt}   5.558026 {c |}{res}          1        0.51        3.06
{txt}   5.588337 {c |}{res}          1        0.51        3.57
{txt}   5.665566 {c |}{res}          1        0.51        4.08
{txt}   5.744206 {c |}{res}          1        0.51        4.59
{txt}   5.782247 {c |}{res}          1        0.51        5.10
{txt}   5.827513 {c |}{res}          1        0.51        5.61
{txt}   5.948795 {c |}{res}          1        0.51        6.12
{txt}   6.142226 {c |}{res}          1        0.51        6.63
{txt}   6.203708 {c |}{res}          1        0.51        7.14
{txt}   6.341821 {c |}{res}          1        0.51        7.65
{txt}   6.378699 {c |}{res}          1        0.51        8.16
{txt}   6.379974 {c |}{res}          1        0.51        8.67
{txt}    6.48762 {c |}{res}          1        0.51        9.18
{txt}   6.609136 {c |}{res}          1        0.51        9.69
{txt}   6.656347 {c |}{res}          1        0.51       10.20
{txt}   6.823926 {c |}{res}          1        0.51       10.71
{txt}   6.847554 {c |}{res}          1        0.51       11.22
{txt}   6.894114 {c |}{res}          1        0.51       11.73
{txt}   7.265425 {c |}{res}          1        0.51       12.24
{txt}   7.324275 {c |}{res}          1        0.51       12.76
{txt}   7.324455 {c |}{res}          1        0.51       13.27
{txt}   7.351899 {c |}{res}          1        0.51       13.78
{txt}   7.471264 {c |}{res}          1        0.51       14.29
{txt}    7.70115 {c |}{res}          1        0.51       14.80
{txt}   7.793938 {c |}{res}          1        0.51       15.31
{txt}   7.969294 {c |}{res}          1        0.51       15.82
{txt}   8.324245 {c |}{res}          1        0.51       16.33
{txt}   8.466914 {c |}{res}          1        0.51       16.84
{txt}   8.601286 {c |}{res}          1        0.51       17.35
{txt}   8.609959 {c |}{res}          1        0.51       17.86
{txt}   8.627019 {c |}{res}          1        0.51       18.37
{txt}   8.682562 {c |}{res}          1        0.51       18.88
{txt}    8.73034 {c |}{res}          1        0.51       19.39
{txt}   8.758983 {c |}{res}          1        0.51       19.90
{txt}   8.783487 {c |}{res}          1        0.51       20.41
{txt}   8.836478 {c |}{res}          1        0.51       20.92
{txt}   8.892764 {c |}{res}          1        0.51       21.43
{txt}   8.896227 {c |}{res}          1        0.51       21.94
{txt}   9.004475 {c |}{res}          1        0.51       22.45
{txt}   9.009726 {c |}{res}          1        0.51       22.96
{txt}    9.02569 {c |}{res}          1        0.51       23.47
{txt}    9.21875 {c |}{res}          1        0.51       23.98
{txt}   9.534706 {c |}{res}          1        0.51       24.49
{txt}   9.704142 {c |}{res}          1        0.51       25.00
{txt}   9.974982 {c |}{res}          1        0.51       25.51
{txt}   10.01372 {c |}{res}          1        0.51       26.02
{txt}   10.20877 {c |}{res}          1        0.51       26.53
{txt}   10.41033 {c |}{res}          1        0.51       27.04
{txt}   10.77608 {c |}{res}          1        0.51       27.55
{txt}    10.8049 {c |}{res}          1        0.51       28.06
{txt}   11.05808 {c |}{res}          1        0.51       28.57
{txt}   11.09223 {c |}{res}          1        0.51       29.08
{txt}   11.13233 {c |}{res}          1        0.51       29.59
{txt}   11.15149 {c |}{res}          1        0.51       30.10
{txt}   11.19276 {c |}{res}          1        0.51       30.61
{txt}   11.30089 {c |}{res}          1        0.51       31.12
{txt}   11.38602 {c |}{res}          1        0.51       31.63
{txt}   11.40651 {c |}{res}          1        0.51       32.14
{txt}   11.56135 {c |}{res}          3        1.53       33.67
{txt}   11.57124 {c |}{res}          1        0.51       34.18
{txt}   11.73359 {c |}{res}          1        0.51       34.69
{txt}   11.93131 {c |}{res}          1        0.51       35.20
{txt}   12.08872 {c |}{res}          1        0.51       35.71
{txt}   12.13382 {c |}{res}          1        0.51       36.22
{txt}   12.15002 {c |}{res}          1        0.51       36.73
{txt}   12.56574 {c |}{res}          1        0.51       37.24
{txt}   12.66482 {c |}{res}          1        0.51       37.76
{txt}   12.72903 {c |}{res}          1        0.51       38.27
{txt}    12.7789 {c |}{res}          1        0.51       38.78
{txt}   12.85632 {c |}{res}          1        0.51       39.29
{txt}   13.03939 {c |}{res}          1        0.51       39.80
{txt}   13.06617 {c |}{res}          1        0.51       40.31
{txt}   13.31029 {c |}{res}          1        0.51       40.82
{txt}   13.33925 {c |}{res}          1        0.51       41.33
{txt}   13.56004 {c |}{res}          1        0.51       41.84
{txt}   13.69838 {c |}{res}          1        0.51       42.35
{txt}   14.40758 {c |}{res}          1        0.51       42.86
{txt}   14.57743 {c |}{res}          1        0.51       43.37
{txt}   14.78487 {c |}{res}          1        0.51       43.88
{txt}   14.84298 {c |}{res}          1        0.51       44.39
{txt}    14.9789 {c |}{res}          1        0.51       44.90
{txt}   15.30457 {c |}{res}          1        0.51       45.41
{txt}   15.88036 {c |}{res}          1        0.51       45.92
{txt}   16.06183 {c |}{res}          1        0.51       46.43
{txt}   16.09317 {c |}{res}          1        0.51       46.94
{txt}   16.10687 {c |}{res}          1        0.51       47.45
{txt}    16.4426 {c |}{res}          1        0.51       47.96
{txt}   16.59288 {c |}{res}          1        0.51       48.47
{txt}   16.67826 {c |}{res}          1        0.51       48.98
{txt}   17.06995 {c |}{res}          1        0.51       49.49
{txt}   17.12469 {c |}{res}          1        0.51       50.00
{txt}   17.39032 {c |}{res}          1        0.51       50.51
{txt}   17.60913 {c |}{res}          1        0.51       51.02
{txt}   17.88679 {c |}{res}          1        0.51       51.53
{txt}   18.02156 {c |}{res}          1        0.51       52.04
{txt}   18.16753 {c |}{res}          1        0.51       52.55
{txt}   18.27068 {c |}{res}          1        0.51       53.06
{txt}   18.31987 {c |}{res}          1        0.51       53.57
{txt}   18.69436 {c |}{res}          1        0.51       54.08
{txt}   19.36275 {c |}{res}          1        0.51       54.59
{txt}   19.56783 {c |}{res}          1        0.51       55.10
{txt}   19.60294 {c |}{res}          1        0.51       55.61
{txt}   19.94762 {c |}{res}          1        0.51       56.12
{txt}    19.9504 {c |}{res}          1        0.51       56.63
{txt}   20.03012 {c |}{res}          1        0.51       57.14
{txt}   20.08876 {c |}{res}          1        0.51       57.65
{txt}   20.09858 {c |}{res}          1        0.51       58.16
{txt}   20.45772 {c |}{res}          1        0.51       58.67
{txt}   20.95629 {c |}{res}          1        0.51       59.18
{txt}   21.03387 {c |}{res}          1        0.51       59.69
{txt}   21.44366 {c |}{res}          1        0.51       60.20
{txt}   21.62921 {c |}{res}          1        0.51       60.71
{txt}   21.74078 {c |}{res}          1        0.51       61.22
{txt}   22.03437 {c |}{res}          1        0.51       61.73
{txt}   22.04428 {c |}{res}          1        0.51       62.24
{txt}   22.08883 {c |}{res}          1        0.51       62.76
{txt}   22.18075 {c |}{res}          1        0.51       63.27
{txt}   22.64957 {c |}{res}          1        0.51       63.78
{txt}     22.881 {c |}{res}          1        0.51       64.29
{txt}   23.09343 {c |}{res}          1        0.51       64.80
{txt}   23.71778 {c |}{res}          1        0.51       65.31
{txt}   25.60482 {c |}{res}          1        0.51       65.82
{txt}   25.73754 {c |}{res}          1        0.51       66.33
{txt}   26.43227 {c |}{res}          1        0.51       66.84
{txt}   27.03901 {c |}{res}          1        0.51       67.35
{txt}   27.04613 {c |}{res}          1        0.51       67.86
{txt}   27.35581 {c |}{res}          1        0.51       68.37
{txt}   28.43091 {c |}{res}          1        0.51       68.88
{txt}   28.62191 {c |}{res}          1        0.51       69.39
{txt}   29.17262 {c |}{res}          1        0.51       69.90
{txt}   29.34132 {c |}{res}          1        0.51       70.41
{txt}   29.41355 {c |}{res}          1        0.51       70.92
{txt}   29.54252 {c |}{res}          1        0.51       71.43
{txt}   29.57418 {c |}{res}          1        0.51       71.94
{txt}   29.95868 {c |}{res}          1        0.51       72.45
{txt}   30.08969 {c |}{res}          1        0.51       72.96
{txt}   30.17544 {c |}{res}          1        0.51       73.47
{txt}   30.31858 {c |}{res}          1        0.51       73.98
{txt}   31.02708 {c |}{res}          1        0.51       74.49
{txt}   31.14862 {c |}{res}          1        0.51       75.00
{txt}   31.14905 {c |}{res}          1        0.51       75.51
{txt}   31.57153 {c |}{res}          1        0.51       76.02
{txt}   31.68574 {c |}{res}          1        0.51       76.53
{txt}   31.78846 {c |}{res}          1        0.51       77.04
{txt}   32.01919 {c |}{res}          1        0.51       77.55
{txt}   32.36704 {c |}{res}          1        0.51       78.06
{txt}   33.54707 {c |}{res}          1        0.51       78.57
{txt}   33.70221 {c |}{res}          1        0.51       79.08
{txt}   33.98383 {c |}{res}          1        0.51       79.59
{txt}   36.25887 {c |}{res}          1        0.51       80.10
{txt}   37.18285 {c |}{res}          1        0.51       80.61
{txt}   37.30159 {c |}{res}          1        0.51       81.12
{txt}   37.42142 {c |}{res}          1        0.51       81.63
{txt}   37.64045 {c |}{res}          1        0.51       82.14
{txt}   37.76138 {c |}{res}          1        0.51       82.65
{txt}   37.98932 {c |}{res}          1        0.51       83.16
{txt}    38.0728 {c |}{res}          1        0.51       83.67
{txt}   38.11314 {c |}{res}          1        0.51       84.18
{txt}   38.73182 {c |}{res}          1        0.51       84.69
{txt}    38.9313 {c |}{res}          1        0.51       85.20
{txt}   40.25631 {c |}{res}          1        0.51       85.71
{txt}   40.73034 {c |}{res}          1        0.51       86.22
{txt}   41.19705 {c |}{res}          1        0.51       86.73
{txt}   43.09162 {c |}{res}          1        0.51       87.24
{txt}   43.88445 {c |}{res}          1        0.51       87.76
{txt}   44.31842 {c |}{res}          1        0.51       88.27
{txt}   44.78846 {c |}{res}          1        0.51       88.78
{txt}    45.4403 {c |}{res}          1        0.51       89.29
{txt}   45.74367 {c |}{res}          1        0.51       89.80
{txt}   45.94192 {c |}{res}          1        0.51       90.31
{txt}   46.14896 {c |}{res}          1        0.51       90.82
{txt}   46.23487 {c |}{res}          1        0.51       91.33
{txt}   46.26194 {c |}{res}          1        0.51       91.84
{txt}   46.32718 {c |}{res}          1        0.51       92.35
{txt}   47.63591 {c |}{res}          1        0.51       92.86
{txt}   47.65139 {c |}{res}          1        0.51       93.37
{txt}   47.77448 {c |}{res}          1        0.51       93.88
{txt}   48.48625 {c |}{res}          1        0.51       94.39
{txt}   49.21658 {c |}{res}          1        0.51       94.90
{txt}   51.48296 {c |}{res}          1        0.51       95.41
{txt}   51.65957 {c |}{res}          1        0.51       95.92
{txt}   52.65597 {c |}{res}          1        0.51       96.43
{txt}   54.52138 {c |}{res}          1        0.51       96.94
{txt}     55.521 {c |}{res}          2        1.02       97.96
{txt}   58.74673 {c |}{res}          1        0.51       98.47
{txt}   64.38253 {c |}{res}          1        0.51       98.98
{txt}   95.18296 {c |}{res}          1        0.51       99.49
{txt}        100 {c |}{res}          1        0.51      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}        196      100.00
{txt}
{com}. *about 5% are winners.  
. *Are any incumbents?  They may not actually be running.  
. tab tempvoteper exper if partyt=="writein"

{txt}tempvotepe {c |}       prior legislative experience of candidate
         r {c |}       inc       none      other    pastinc  pastother {c |}     Total
{hline 11}{c +}{hline 55}{c +}{hline 10}
  5.136268 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  5.234796 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  5.304237 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  5.453155 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  5.534591 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  5.558026 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  5.588337 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  5.665566 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  5.744206 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  5.782247 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  5.827513 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  5.948795 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  6.142226 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  6.203708 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  6.341821 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  6.378699 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  6.379974 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}   6.48762 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  6.609136 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  6.656347 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  6.823926 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  6.847554 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  6.894114 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  7.265425 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  7.324275 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  7.324455 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  7.351899 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  7.471264 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}   7.70115 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  7.793938 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  7.969294 {c |}{res}         0          0          1          0          0 {txt}{c |}{res}         1 
{txt}  8.324245 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  8.466914 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  8.601286 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  8.609959 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  8.627019 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  8.682562 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}   8.73034 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  8.758983 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  8.783487 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  8.836478 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  8.892764 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  8.896227 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  9.004475 {c |}{res}         0          0          0          1          0 {txt}{c |}{res}         1 
{txt}  9.009726 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}   9.02569 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}   9.21875 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  9.534706 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  9.704142 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  9.974982 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  10.01372 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  10.20877 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  10.41033 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  10.77608 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}   10.8049 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  11.05808 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  11.09223 {c |}{res}         0          0          1          0          0 {txt}{c |}{res}         1 
{txt}  11.13233 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  11.15149 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  11.19276 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  11.30089 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  11.38602 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  11.40651 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  11.56135 {c |}{res}         0          0          0          0          3 {txt}{c |}{res}         3 
{txt}  11.57124 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  11.73359 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  11.93131 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  12.08872 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  12.13382 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  12.15002 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  12.56574 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  12.66482 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  12.72903 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}   12.7789 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  12.85632 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  13.03939 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  13.06617 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  13.31029 {c |}{res}         0          0          0          0          1 {txt}{c |}{res}         1 
{txt}  13.33925 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  13.56004 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  13.69838 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  14.40758 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  14.57743 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  14.78487 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  14.84298 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}   14.9789 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  15.30457 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  15.88036 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  16.06183 {c |}{res}         0          0          0          1          0 {txt}{c |}{res}         1 
{txt}  16.09317 {c |}{res}         0          0          0          1          0 {txt}{c |}{res}         1 
{txt}  16.10687 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}   16.4426 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  16.59288 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  16.67826 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  17.06995 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  17.12469 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  17.39032 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  17.60913 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  17.88679 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  18.02156 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  18.16753 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  18.27068 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  18.31987 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  18.69436 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  19.36275 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  19.56783 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  19.60294 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  19.94762 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}   19.9504 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  20.03012 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  20.08876 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  20.09858 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  20.45772 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  20.95629 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  21.03387 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  21.44366 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  21.62921 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  21.74078 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  22.03437 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  22.04428 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  22.08883 {c |}{res}         0          0          0          1          0 {txt}{c |}{res}         1 
{txt}  22.18075 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  22.64957 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}    22.881 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  23.09343 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  23.71778 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  25.60482 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  25.73754 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  26.43227 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  27.03901 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  27.04613 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  27.35581 {c |}{res}         0          0          1          0          0 {txt}{c |}{res}         1 
{txt}  28.43091 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  28.62191 {c |}{res}         0          0          0          1          0 {txt}{c |}{res}         1 
{txt}  29.17262 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  29.34132 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  29.41355 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  29.54252 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  29.57418 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  29.95868 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  30.08969 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  30.17544 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  30.31858 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  31.02708 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  31.14862 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  31.14905 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  31.57153 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  31.68574 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  31.78846 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  32.01919 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  32.36704 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  33.54707 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  33.70221 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  33.98383 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  36.25887 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  37.18285 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  37.30159 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  37.42142 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  37.64045 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  37.76138 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  37.98932 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}   38.0728 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  38.11314 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  38.73182 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}   38.9313 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  40.25631 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  40.73034 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  41.19705 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  43.09162 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  43.88445 {c |}{res}         0          0          0          1          0 {txt}{c |}{res}         1 
{txt}  44.31842 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  44.78846 {c |}{res}         0          0          0          1          0 {txt}{c |}{res}         1 
{txt}   45.4403 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  45.74367 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  45.94192 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  46.14896 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  46.23487 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  46.26194 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  46.32718 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  47.63591 {c |}{res}         0          0          1          0          0 {txt}{c |}{res}         1 
{txt}  47.65139 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  47.77448 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  48.48625 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  49.21658 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  51.48296 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}  51.65957 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  52.65597 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  54.52138 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}    55.521 {c |}{res}         2          0          0          0          0 {txt}{c |}{res}         2 
{txt}  58.74673 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  64.38253 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}  95.18296 {c |}{res}         0          1          0          0          0 {txt}{c |}{res}         1 
{txt}       100 {c |}{res}         1          0          0          0          0 {txt}{c |}{res}         1 
{txt}{hline 11}{c +}{hline 55}{c +}{hline 10}
     Total {c |}{res}        65        116          4          7          4 {txt}{c |}{res}       196 
{txt}
{com}. gen quality=exper!="none"
{txt}
{com}. tab quality if partyt=="writein"

    {txt}quality {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}        116       59.18       59.18
{txt}          1 {c |}{res}         80       40.82      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}        196      100.00
{txt}
{com}. logit quality tempvoteper if partyt=="writein"

{res}{txt}Iteration 0:{space 3}log likelihood = {res:-132.53188}  
Iteration 1:{space 3}log likelihood = {res:-127.67056}  
Iteration 2:{space 3}log likelihood = {res:-127.66207}  
Iteration 3:{space 3}log likelihood = {res:-127.66207}  
{res}
{txt}Logistic regression{col 49}Number of obs{col 67}= {res}       196
{txt}{col 49}LR chi2({res}1{txt}){col 67}= {res}      9.74
{txt}{col 49}Prob > chi2{col 67}= {res}    0.0018
{txt}Log likelihood = {res}-127.66207{txt}{col 49}Pseudo R2{col 67}= {res}    0.0367

{txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}
{col 1}     quality{col 14}{c |}      Coef.{col 26}   Std. Err.{col 38}      z{col 46}   P>|z|{col 54}     [95% Con{col 67}f. Interval]
{hline 13}{c +}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}
{space 1}tempvoteper {c |}{col 14}{res}{space 2} .0292278{col 26}{space 2}  .009825{col 37}{space 1}    2.97{col 46}{space 3}0.003{col 54}{space 4} .0099713{col 67}{space 3} .0484844
{txt}{space 7}_cons {c |}{col 14}{res}{space 2}-1.030418{col 26}{space 2} .2667249{col 37}{space 1}   -3.86{col 46}{space 3}0.000{col 54}{space 4}-1.553189{col 67}{space 3} -.507647
{txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}

{com}. *80 out of 196 are incumbents or prior office holders, and the writein in question is more likely to be a prior office holder if they received a larger percent of the vote they obtained in the election.  Nothing definitive can be inferred about which of these are "real" candidates from this.  
. drop tempvoteper quality
{txt}
{com}. *I judged the following person wasn't actually on the ballot, although they were reported as having 0 votes in the returns.  
. drop if year==2002&sid==8&sen==0&dno==9&partyz=="d"
{txt}(0 observations deleted)

{com}. *They're gone anyway, no obs dropped.  
. 
. *VOTEMISS
. replace votemiss=vote==.
{txt}(0 real changes made)

{com}. 
. *FIRSTCASE
. recode firstcase (1/2=1)
{txt}(firstcase: 889 changes made)

{com}. replace firstcase=1 if sid==23&(year==1974|(year==1976&sen==1&dno!=47&dno!=64))
{txt}(392 real changes made)

{com}. 
. *UOA CAND
. collapse (mean) votemiss (sum) vote (max) dontuse firstcase, by(year sid sen dname dno geopost mmdpost specpost cand candid termz outcome exper tenure1 tenure2 deter etype eseats dseats dtype popnum regime redist redist1 redist2 redist3 nest nest1 nest2 nest3 partyt)
{txt}
{com}. 
. *VOTEMISS
. *verify that votemiss is either 0 or 1, and not in between.
. assert votemiss==0|votemiss==1
{txt}
{com}. *that is correct
. *replace vote=0 with system missing to deal with Stata's problem with this.  
. replace vote=. if votemiss==1
{txt}(5,647 real changes made, 5,647 to missing)

{com}. 
. *IDENTIFICATION
. *verify that each candidate is only observed once per election.  
. bysort year sid sen dname dno geopost mmdpost etype cand candid: assert _N==1
{txt}
{com}. *all 1, good
. 
. *PARTYWEIRD
. *Create a variable tracking party switches since the last time they ran, as long as it was within four years, and from what party to what.  
. *don't consider it a party switch if they were a write-in or non-major party candidate in the past.  I'm going to assume that if someone runs as a dem or repub after being a third party candidate, that won't effect the vote they obtain.
. tab vote if partyt=="writein"

 {txt}(sum) vote {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
         43 {c |}{res}          1        0.52        0.52
{txt}         68 {c |}{res}          1        0.52        1.04
{txt}         79 {c |}{res}          1        0.52        1.55
{txt}         82 {c |}{res}          1        0.52        2.07
{txt}        103 {c |}{res}          1        0.52        2.59
{txt}        121 {c |}{res}          1        0.52        3.11
{txt}        125 {c |}{res}          1        0.52        3.63
{txt}        137 {c |}{res}          1        0.52        4.15
{txt}        153 {c |}{res}          1        0.52        4.66
{txt}        154 {c |}{res}          1        0.52        5.18
{txt}        161 {c |}{res}          1        0.52        5.70
{txt}        177 {c |}{res}          1        0.52        6.22
{txt}        184 {c |}{res}          1        0.52        6.74
{txt}        198 {c |}{res}          1        0.52        7.25
{txt}        201 {c |}{res}          1        0.52        7.77
{txt}        211 {c |}{res}          1        0.52        8.29
{txt}        225 {c |}{res}          1        0.52        8.81
{txt}        229 {c |}{res}          1        0.52        9.33
{txt}        235 {c |}{res}          1        0.52        9.84
{txt}        237 {c |}{res}          1        0.52       10.36
{txt}        243 {c |}{res}          1        0.52       10.88
{txt}        245 {c |}{res}          1        0.52       11.40
{txt}        247 {c |}{res}          1        0.52       11.92
{txt}        250 {c |}{res}          1        0.52       12.44
{txt}        264 {c |}{res}          1        0.52       12.95
{txt}        268 {c |}{res}          1        0.52       13.47
{txt}        281 {c |}{res}          1        0.52       13.99
{txt}        290 {c |}{res}          1        0.52       14.51
{txt}        305 {c |}{res}          1        0.52       15.03
{txt}        306 {c |}{res}          1        0.52       15.54
{txt}        308 {c |}{res}          1        0.52       16.06
{txt}        322 {c |}{res}          1        0.52       16.58
{txt}        326 {c |}{res}          1        0.52       17.10
{txt}        327 {c |}{res}          1        0.52       17.62
{txt}        333 {c |}{res}          1        0.52       18.13
{txt}        335 {c |}{res}          1        0.52       18.65
{txt}        353 {c |}{res}          1        0.52       19.17
{txt}        367 {c |}{res}          1        0.52       19.69
{txt}        383 {c |}{res}          1        0.52       20.21
{txt}        388 {c |}{res}          2        1.04       21.24
{txt}        396 {c |}{res}          1        0.52       21.76
{txt}        403 {c |}{res}          1        0.52       22.28
{txt}        405 {c |}{res}          1        0.52       22.80
{txt}        408 {c |}{res}          2        1.04       23.83
{txt}        409 {c |}{res}          1        0.52       24.35
{txt}        427 {c |}{res}          1        0.52       24.87
{txt}        428 {c |}{res}          1        0.52       25.39
{txt}        437 {c |}{res}          1        0.52       25.91
{txt}        446 {c |}{res}          1        0.52       26.42
{txt}        448 {c |}{res}          1        0.52       26.94
{txt}        472 {c |}{res}          1        0.52       27.46
{txt}        491 {c |}{res}          1        0.52       27.98
{txt}        568 {c |}{res}          1        0.52       28.50
{txt}        578 {c |}{res}          1        0.52       29.02
{txt}        591 {c |}{res}          1        0.52       29.53
{txt}        598 {c |}{res}          1        0.52       30.05
{txt}        607 {c |}{res}          1        0.52       30.57
{txt}        638 {c |}{res}          1        0.52       31.09
{txt}        671 {c |}{res}          1        0.52       31.61
{txt}        686 {c |}{res}          1        0.52       32.12
{txt}        702 {c |}{res}          1        0.52       32.64
{txt}        712 {c |}{res}          1        0.52       33.16
{txt}        726 {c |}{res}          1        0.52       33.68
{txt}        729 {c |}{res}          1        0.52       34.20
{txt}        754 {c |}{res}          1        0.52       34.72
{txt}        763 {c |}{res}          1        0.52       35.23
{txt}        765 {c |}{res}          1        0.52       35.75
{txt}        773 {c |}{res}          1        0.52       36.27
{txt}        784 {c |}{res}          1        0.52       36.79
{txt}        796 {c |}{res}          1        0.52       37.31
{txt}        805 {c |}{res}          1        0.52       37.82
{txt}        832 {c |}{res}          1        0.52       38.34
{txt}        842 {c |}{res}          1        0.52       38.86
{txt}        847 {c |}{res}          1        0.52       39.38
{txt}        850 {c |}{res}          1        0.52       39.90
{txt}        855 {c |}{res}          1        0.52       40.41
{txt}        882 {c |}{res}          1        0.52       40.93
{txt}        884 {c |}{res}          1        0.52       41.45
{txt}        889 {c |}{res}          1        0.52       41.97
{txt}        894 {c |}{res}          1        0.52       42.49
{txt}        902 {c |}{res}          1        0.52       43.01
{txt}        910 {c |}{res}          1        0.52       43.52
{txt}        916 {c |}{res}          1        0.52       44.04
{txt}        956 {c |}{res}          1        0.52       44.56
{txt}        978 {c |}{res}          1        0.52       45.08
{txt}        982 {c |}{res}          1        0.52       45.60
{txt}        996 {c |}{res}          1        0.52       46.11
{txt}       1007 {c |}{res}          1        0.52       46.63
{txt}       1038 {c |}{res}          1        0.52       47.15
{txt}       1065 {c |}{res}          1        0.52       47.67
{txt}       1073 {c |}{res}          1        0.52       48.19
{txt}       1089 {c |}{res}          1        0.52       48.70
{txt}       1096 {c |}{res}          1        0.52       49.22
{txt}       1108 {c |}{res}          1        0.52       49.74
{txt}       1119 {c |}{res}          1        0.52       50.26
{txt}       1144 {c |}{res}          1        0.52       50.78
{txt}       1163 {c |}{res}          1        0.52       51.30
{txt}       1191 {c |}{res}          1        0.52       51.81
{txt}       1202 {c |}{res}          1        0.52       52.33
{txt}       1203 {c |}{res}          1        0.52       52.85
{txt}       1205 {c |}{res}          1        0.52       53.37
{txt}       1218 {c |}{res}          1        0.52       53.89
{txt}       1219 {c |}{res}          1        0.52       54.40
{txt}       1247 {c |}{res}          1        0.52       54.92
{txt}       1254 {c |}{res}          1        0.52       55.44
{txt}       1287 {c |}{res}          1        0.52       55.96
{txt}       1293 {c |}{res}          1        0.52       56.48
{txt}       1315 {c |}{res}          1        0.52       56.99
{txt}       1321 {c |}{res}          1        0.52       57.51
{txt}       1328 {c |}{res}          1        0.52       58.03
{txt}       1418 {c |}{res}          1        0.52       58.55
{txt}       1420 {c |}{res}          1        0.52       59.07
{txt}       1445 {c |}{res}          1        0.52       59.59
{txt}       1472 {c |}{res}          1        0.52       60.10
{txt}       1503 {c |}{res}          1        0.52       60.62
{txt}       1516 {c |}{res}          1        0.52       61.14
{txt}       1535 {c |}{res}          1        0.52       61.66
{txt}       1555 {c |}{res}          1        0.52       62.18
{txt}       1575 {c |}{res}          1        0.52       62.69
{txt}       1577 {c |}{res}          1        0.52       63.21
{txt}       1632 {c |}{res}          1        0.52       63.73
{txt}       1646 {c |}{res}          1        0.52       64.25
{txt}       1732 {c |}{res}          1        0.52       64.77
{txt}       1789 {c |}{res}          1        0.52       65.28
{txt}       1825 {c |}{res}          1        0.52       65.80
{txt}       1841 {c |}{res}          1        0.52       66.32
{txt}       1855 {c |}{res}          1        0.52       66.84
{txt}       1869 {c |}{res}          1        0.52       67.36
{txt}       1936 {c |}{res}          1        0.52       67.88
{txt}       1983 {c |}{res}          1        0.52       68.39
{txt}       1995 {c |}{res}          1        0.52       68.91
{txt}       2012 {c |}{res}          1        0.52       69.43
{txt}       2051 {c |}{res}          1        0.52       69.95
{txt}       2063 {c |}{res}          1        0.52       70.47
{txt}       2150 {c |}{res}          1        0.52       70.98
{txt}       2257 {c |}{res}          1        0.52       71.50
{txt}       2285 {c |}{res}          1        0.52       72.02
{txt}       2362 {c |}{res}          1        0.52       72.54
{txt}       2499 {c |}{res}          1        0.52       73.06
{txt}       2610 {c |}{res}          1        0.52       73.58
{txt}       2801 {c |}{res}          1        0.52       74.09
{txt}       2814 {c |}{res}          1        0.52       74.61
{txt}       2856 {c |}{res}          1        0.52       75.13
{txt}       3008 {c |}{res}          1        0.52       75.65
{txt}       3045 {c |}{res}          1        0.52       76.17
{txt}       3047 {c |}{res}          1        0.52       76.68
{txt}       3078 {c |}{res}          1        0.52       77.20
{txt}       3084 {c |}{res}          1        0.52       77.72
{txt}       3085 {c |}{res}          1        0.52       78.24
{txt}       3139 {c |}{res}          1        0.52       78.76
{txt}       3175 {c |}{res}          1        0.52       79.27
{txt}       3203 {c |}{res}          1        0.52       79.79
{txt}       3290 {c |}{res}          1        0.52       80.31
{txt}       3774 {c |}{res}          1        0.52       80.83
{txt}       3841 {c |}{res}          1        0.52       81.35
{txt}       3895 {c |}{res}          1        0.52       81.87
{txt}       3940 {c |}{res}          1        0.52       82.38
{txt}       4115 {c |}{res}          1        0.52       82.90
{txt}       4118 {c |}{res}          1        0.52       83.42
{txt}       4254 {c |}{res}          1        0.52       83.94
{txt}       4259 {c |}{res}          1        0.52       84.46
{txt}       4350 {c |}{res}          1        0.52       84.97
{txt}       4355 {c |}{res}          1        0.52       85.49
{txt}       4371 {c |}{res}          1        0.52       86.01
{txt}       4701 {c |}{res}          1        0.52       86.53
{txt}       4756 {c |}{res}          1        0.52       87.05
{txt}       4836 {c |}{res}          1        0.52       87.56
{txt}       4999 {c |}{res}          1        0.52       88.08
{txt}       5202 {c |}{res}          1        0.52       88.60
{txt}       5219 {c |}{res}          1        0.52       89.12
{txt}       5251 {c |}{res}          1        0.52       89.64
{txt}       5368 {c |}{res}          1        0.52       90.16
{txt}       5484 {c |}{res}          1        0.52       90.67
{txt}       5681 {c |}{res}          1        0.52       91.19
{txt}       5699 {c |}{res}          1        0.52       91.71
{txt}       5715 {c |}{res}          1        0.52       92.23
{txt}       5719 {c |}{res}          1        0.52       92.75
{txt}       6119 {c |}{res}          1        0.52       93.26
{txt}       6543 {c |}{res}          1        0.52       93.78
{txt}       7206 {c |}{res}          1        0.52       94.30
{txt}       7497 {c |}{res}          1        0.52       94.82
{txt}       8000 {c |}{res}          1        0.52       95.34
{txt}       8469 {c |}{res}          1        0.52       95.85
{txt}      11615 {c |}{res}          1        0.52       96.37
{txt}      11818 {c |}{res}          1        0.52       96.89
{txt}      22805 {c |}{res}          1        0.52       97.41
{txt}      23011 {c |}{res}          1        0.52       97.93
{txt}      25598 {c |}{res}          1        0.52       98.45
{txt}      26968 {c |}{res}          1        0.52       98.96
{txt}      30252 {c |}{res}          1        0.52       99.48
{txt}      34860 {c |}{res}          1        0.52      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}        193      100.00
{txt}
{com}. sum vote if partyt=="writein"

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 8}vote {c |}{res}        193    2641.306    4922.237         43      34860
{txt}
{com}. *most of those obtained large numbers of votes, and the ave is about 2.6k.  
. *The following is because it doesn't matter if someone went from nonmaj to partymiss.  If they go from dem to partymiss, that would still register.  
. gen partyt2=partyt
{txt}
{com}. replace partyt2="nonmaj" if partyt=="nonpart"|partyt=="partymiss"
{txt}(249 real changes made)

{com}. bysort candid (year): gen id=1 if _n==1
{txt}(147,394 missing values generated)

{com}. replace id=sum(id)
{txt}(269,464 real changes made)

{com}. bysort id (year): gen row=_n
{txt}
{com}. tsset id row
{res}{txt}{col 8}panel variable:  {res}id (unbalanced)
{txt}{col 9}time variable:  {res}{col 25}row, 1 to 25
{txt}{col 17}delta:  {res}1 unit
{txt}
{com}. by id: gen lag=partyt2[_n-1]
{txt}(122,071 missing values generated)

{com}. by id: gen yearlag=year[_n-1]
{txt}(122,071 missing values generated)

{com}. gen yeardif=year-yearlag
{txt}(122,071 missing values generated)

{com}. *"switch" tracks whether there has been a d to r or r to d switch.
. gen dswitch=partyt2=="d"&lag=="r"&yeardif<5
{txt}
{com}. gen rswitch=partyt2=="r"&lag=="d"&yeardif<5
{txt}
{com}. *"stealth" tracks whether a non-major party cand was a d or r in the recent past, even if they're a writein (only writeins with a lot of votes are left in at this point).  
. gen dstealth=partyt2=="nonmaj"&lag=="d"&yeardif<5
{txt}
{com}. gen rstealth=partyt2=="nonmaj"&lag=="r"&yeardif<5
{txt}
{com}. by id: gen outcomelag=outcome[_n-1]
{txt}(122,071 missing values generated)

{com}. gen dswitchwin=dswitch==1&outcomelag=="w"
{txt}
{com}. gen rswitchwin=rswitch==1&outcomelag=="w"
{txt}
{com}. gen dstealthwin=dstealth==1&outcomelag=="w"
{txt}
{com}. gen rstealthwin=rstealth==1&outcomelag=="w"
{txt}
{com}. drop partyt2 id row lag yearlag yeardif outcomelag
{txt}
{com}. *DESCRIPTIVES
. *% of contests
. *party switchers: .58
. *party switchers who won last time: .45
. *stealth partisans: .39
. *stealth partisans who won last time: .21
. *Total: .98% of all contests have some kind of party weirdness, and .66 have some kind of party weirdness with a winner from the past.  
. *CUMULATIVE CUTS
. gen cumulativecuts=0
{txt}
{com}. foreach string in dswitch rswitch dstealth rstealth dswitchwin rswitchwin dstealthwin rstealthwin {c -(}
{txt}  2{com}. replace cumulativecuts=1 if `string'==1
{txt}  3{com}. {c )-}
{txt}(295 real changes made)
(489 real changes made)
(283 real changes made)
(286 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)

{com}. 
. *KEYVARS
. *create variables tracking cands, wins, incs, inc2s, inc3s, votes and prior legislative experience.
. rename cand candname
{res}{txt}
{com}. gen cand=1
{txt}
{com}. gen win=outcome=="w"
{txt}
{com}. gen inc=exper=="inc"
{txt}
{com}. recode tenure1 (0/3=0) (4/7=1) (8/max=0), gen(inc2)
{txt}(121634 differences between tenure1 and inc2)

{com}. recode tenure1 (0/7=0) (8/max=1), gen(inc3)
{txt}(121634 differences between tenure1 and inc3)

{com}. recode tenure2 (0/3=0) (4/7=1) (8/max=0), gen(leg2)
{txt}(126085 differences between tenure2 and leg2)

{com}. recode tenure2 (0/7=0) (8/max=1), gen(leg3)
{txt}(126085 differences between tenure2 and leg3)

{com}. gen other=exper=="other"
{txt}
{com}. gen past=exper=="pastinc"|exper=="pastother"|exper=="pastboth"
{txt}
{com}. *pull out # cands, votes and wins by party
. foreach string in cand vote win inc inc2 inc3 other leg2 leg3 past {c -(}
{txt}  2{com}. gen d`string'=`string' if partyt=="d"
{txt}  3{com}. gen r`string'=`string' if partyt=="r"
{txt}  4{com}. gen o`string'=`string' if partyt!="d"&partyt!="r"
{txt}  5{com}. {c )-}
{txt}(133,742 missing values generated)
(147,702 missing values generated)
(257,486 missing values generated)
(137,848 missing values generated)
(149,239 missing values generated)
(257,490 missing values generated)
(133,742 missing values generated)
(147,702 missing values generated)
(257,486 missing values generated)
(133,742 missing values generated)
(147,702 missing values generated)
(257,486 missing values generated)
(133,742 missing values generated)
(147,702 missing values generated)
(257,486 missing values generated)
(133,742 missing values generated)
(147,702 missing values generated)
(257,486 missing values generated)
(133,742 missing values generated)
(147,702 missing values generated)
(257,486 missing values generated)
(133,742 missing values generated)
(147,702 missing values generated)
(257,486 missing values generated)
(133,742 missing values generated)
(147,702 missing values generated)
(257,486 missing values generated)
(133,742 missing values generated)
(147,702 missing values generated)
(257,486 missing values generated)

{com}. drop cand exper inc inc2 inc3 other leg2 leg3 past
{txt}
{com}. 
. *TERMZ
. replace termz=1 if termz==1.5
{txt}(4 real changes made)

{com}. *create weights for termz
. bysort year sid sen dname dno geopost mmdpost etype: gen sum1=_N
{txt}
{com}. bysort year sid sen dname dno geopost mmdpost etype termz: gen sum2=_N
{txt}
{com}. gen dif=sum1-sum2
{txt}
{com}. egen max=max(termz), by(year sid sen dname dno geopost mmdpost etype)
{txt}
{com}. *the following is non-zero for just a few elections
. gen minweight=win==1&termz!=max
{txt}
{com}. *the following is non-zero for all elections.  
. gen maxweight=win==1&(dif==0|(termz==max))
{txt}
{com}. drop sum1 sum2 dif win max
{txt}
{com}. 
. *VOTEMISS
. *The following non-maj cand is sysmis for vote, but can be dropped without hurting anything.  There should be one obs dropped in the following.
. drop if year==2010&sid==44&sen==0&dno==52&partyt=="nonmaj"&vote==.
{txt}(1 observation deleted)

{com}. egen mean=mean(votemiss), by(year sid sen dname dno geopost mmdpost)
{txt}
{com}. list year sid sen dname dno geopost mmdpost partyt votemiss vote outcome dontuse if mean!=0&mean!=1
{txt}
        {c TLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 8}{c -}{hline 10}{c -}{hline 7}{c -}{hline 9}{c -}{hline 9}{c TRC}
        {c |} {res}year   sid   sen   dname   dno   geopost   mmdpost   partyt   votemiss    vote   outcome   dontuse {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 8}{c -}{hline 10}{c -}{hline 7}{c -}{hline 9}{c -}{hline 9}{c RT}
 30910. {c |} {res}1972    44     0            26         .         .        r          1       .         w         0 {txt}{c |}
 30911. {c |} {res}1972    44     0            26         .         .        d          0    2833         l         0 {txt}{c |}
 86525. {c |} {res}1983    24     1            45         .         .        r          1       .         l         0 {txt}{c |}
 86526. {c |} {res}1983    24     1            45         .         .        d          0   10517         w         0 {txt}{c |}
        {c BLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 8}{c -}{hline 10}{c -}{hline 7}{c -}{hline 9}{c -}{hline 9}{c BRC}

{com}. *There are four cases from two such elections.  They should be dontuse=1, bcs both have two cands, one d and one r, and one cand in each election is sysmis for vote.  Taking the max of votemiss will accomplish this when it is done later.  
. sum sid if mean!=0&mean!=1

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 9}sid {c |}{res}          4          34    11.54701         24         44
{txt}
{com}. local aaa=r(N)
{txt}
{com}. assert `aaa'==4
{txt}
{com}. drop mean
{txt}
{com}. save $mainfile, replace
{txt}file 102slersuoacontest20181024.dta saved

{com}. 
. 
. 
. *MAINFILE
. clear
{txt}
{com}. use $mainfile
{txt}
{com}. 
. *UOA CONTEST
. collapse (sum) dvote rvote ovote dcand rcand ocand dwin rwin owin dinc rinc oinc dinc2 rinc2 oinc2 dinc3 rinc3 oinc3 dother rother oother dleg2 rleg2 oleg2 dleg3 rleg3 oleg3 dpast rpast opast dswitch dswitchwin rswitch rswitchwin dstealth rstealth dstealthwin rstealthwin maxweight minweight (max) votemiss dontuse maxtermz=termz firstcase cumulativecuts (min) mintermz=termz, by(year sid sen dname dno geopost mmdpost specpost deter etype eseats dseats dtype popnum regime redist redist1 redist2 redist3 nest nest1 nest2 nest3 specpost)
{txt}
{com}. 
. *VOTEMISS
. *replace vote=0 with system missing to deal with Stata's problem with this.  
. recode dvote rvote ovote (*=.) if votemiss==1
{txt}(dvote: 5575 changes made)
(rvote: 5575 changes made)
(ovote: 5575 changes made)

{com}. 
. *MS HS 1975 #28, GEOPOST=2, MMDPOST=2 is in there twice, once as deter=0&etype=g, and once as deter=1&etype=s.  This was apparently an unresolved general election.  eseats=1.  
. list etype dvote rvote ovote dcand rcand ocand dwin rwin owin if year==1975&sid==24&sen==0&dno==28&geopost==2&mmdpost==2
{txt}
        {c TLC}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 6}{c -}{hline 6}{c -}{hline 6}{c TRC}
        {c |} {res}etype   dvote   rvote   ovote   dcand   rcand   ocand   dwin   rwin   owin {txt}{c |}
        {c LT}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 6}{c -}{hline 6}{c -}{hline 6}{c RT}
 20033. {c |} {res}    g    9571    3275       0       1       1       0      1      0      0 {txt}{c |}
 20034. {c |} {res}    s    4400       0    4097       1       0       4      1      0      0 {txt}{c |}
        {c BLC}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 6}{c -}{hline 6}{c -}{hline 6}{c BRC}

{com}. *For simplicity, just drop the special election.  redist also=0 for the general, which shouldn't be the case.  
. replace redist=1 if year==1975&sid==24&sen==0&dno==28&geopost==2&mmdpost==2
{txt}(1 real change made)

{com}. drop if year==1975&sid==24&sen==0&dno==28&geopost==2&mmdpost==2&etype=="s"
{txt}(1 observation deleted)

{com}. *one obs dropped from each, good
. 
. *IDENTIFICATION
. bysort year sid sen dname dno geopost mmdpost: assert _N==1
{txt}
{com}. 
. *PROPS
. *replace vars with proportions
. *The next four lines are just to make the code work.
. gen oswitch=0
{txt}
{com}. gen ostealth=0
{txt}
{com}. gen oswitchwin=0
{txt}
{com}. gen ostealthwin=0
{txt}
{com}. foreach string in cand win inc inc2 inc3 other leg2 leg3 past switch switchwin stealth stealthwin {c -(}
{txt}  2{com}. foreach party in d r o {c -(}
{txt}  3{com}. *adjust number of cands when election is over-contested.  This will get top two primary state-years and NV, but that's part of the plan, they are uncontested elections.  
. replace `party'`string'=eseats if `party'`string'>eseats&`party'`string'!=.
{txt}  4{com}. replace `party'`string'=`party'`string'/eseats
{txt}  5{com}. {c )-}
{txt}  6{com}. {c )-}
{txt}(115 real changes made)
(9,911 real changes made)
(74 real changes made)
(9,571 real changes made)
(1,239 real changes made)
(1,202 real changes made)
(0 real changes made)
(6,814 real changes made)
(0 real changes made)
(6,731 real changes made)
(0 real changes made)
(66 real changes made)
(0 real changes made)
(6,001 real changes made)
(0 real changes made)
(5,981 real changes made)
(0 real changes made)
(102 real changes made)
(0 real changes made)
(3,012 real changes made)
(0 real changes made)
(3,009 real changes made)
(0 real changes made)
(39 real changes made)
(0 real changes made)
(2,609 real changes made)
(0 real changes made)
(2,272 real changes made)
(0 real changes made)
(30 real changes made)
(1 real change made)
(113 real changes made)
(0 real changes made)
(91 real changes made)
(0 real changes made)
(1 real change made)
(1 real change made)
(3,013 real changes made)
(0 real changes made)
(3,025 real changes made)
(0 real changes made)
(38 real changes made)
(0 real changes made)
(2,699 real changes made)
(0 real changes made)
(2,336 real changes made)
(0 real changes made)
(31 real changes made)
(2 real changes made)
(851 real changes made)
(0 real changes made)
(860 real changes made)
(0 real changes made)
(31 real changes made)
(0 real changes made)
(78 real changes made)
(0 real changes made)
(96 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(55 real changes made)
(0 real changes made)
(57 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(58 real changes made)
(1 real change made)
(46 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(30 real changes made)
(0 real changes made)
(26 real changes made)
(0 real changes made)
(0 real changes made)

{com}. drop oswitch ostealth oswitchwin ostealthwin
{txt}
{com}. *for one party, inc+oth+past can't be more than 1.  Assume incumbents will beat other who will beat past.  That means if inc>1, adjust inc down to 1.  Then if inc+other>1, subtract other by the amount more than 1 that sum is.  Then, if inc+other(new)+past is >1, subtract past by the amount that sum is greater than 1 by.  
. foreach party in d r {c -(}
{txt}  2{com}. gen temp=`party'inc+`party'other-1
{txt}  3{com}. replace temp=0 if temp<0
{txt}  4{com}. replace `party'other=`party'other-temp
{txt}  5{com}. replace `party'other=0 if `party'other<0
{txt}  6{com}. replace temp=`party'inc+`party'other+`party'past-1
{txt}  7{com}. replace temp=0 if temp<0
{txt}  8{com}. replace `party'past=`party'past-temp
{txt}  9{com}. replace `party'past=0 if `party'past<0
{txt} 10{com}. drop temp
{txt} 11{com}. {c )-}
{txt}(78,508 real changes made)
(2 real changes made)
(0 real changes made)
(75,860 real changes made)
(75,852 real changes made)
(6 real changes made)
(0 real changes made)
(93,269 real changes made)
(1 real change made)
(0 real changes made)
(91,014 real changes made)
(91,012 real changes made)
(1 real change made)
(0 real changes made)

{com}. 
. *UNCONT
. *mixeduncont
. *These are contests in which the total number of major party candidates (dems + repubs) equals the number of seats to be won and so the party of the winners is known in advance.  
. gen mixeduncont=((dcand+rcand)==1)&dcand!=1&rcand!=1
{txt}
{com}. *partuncont
. gen partuncont=([(dcand>.01)&(dcand<.99)]|[(rcand>.01)&(rcand<.99)])&dcand!=0&rcand!=0&mixeduncont!=1
{txt}
{com}. assert eseats>1 if partuncont==1
{txt}
{com}. *uncont
. gen uncont=rcand==0|dcand==0|mixeduncont==1
{txt}
{com}. assert uncont==0 if partuncont==1
{txt}
{com}. assert partuncont==0 if uncont==1
{txt}
{com}. *DESCRIPTIVES
. *mixeduncont: .16%
. *partuncont: 2.04%
. *CUMULATIVE CUTS
. replace cumulativecuts=cumulativecuts+1 if mixeduncont==1|partuncont==1
{txt}(3,115 real changes made)

{com}. tab cumulativecuts

      {txt}(max) {c |}
cumulativec {c |}
        uts {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}    135,893       96.90       96.90
{txt}          1 {c |}{res}      4,243        3.03       99.93
{txt}          2 {c |}{res}        101        0.07      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}    140,237      100.00
{txt}
{com}. *CUMULATIVE CUTS: 3.10%
. 
. *VOTESHARE
. gen dper=(dvote/(dvote+rvote))*100
{txt}(5,639 missing values generated)

{com}. replace dper=100 if dwin==1&dcand==1&rcand==0&dontuse==0
{txt}(3,961 real changes made)

{com}. replace dper=0 if rwin==1&dcand==0&rcand==1&dontuse==0
{txt}(1,446 real changes made)

{com}. *If dper is system missing and the election is fully or partially contested by the major parties, make dontuse=1.  
. *First, look at those cases.
. tab sid if dontuse==0&dper==.&uncont==0

  {txt}number of {c |}
   state in {c |}
 alpha list {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          4 {c |}{res}         12       57.14       57.14
{txt}         11 {c |}{res}          1        4.76       61.90
{txt}         17 {c |}{res}          4       19.05       80.95
{txt}         24 {c |}{res}          1        4.76       85.71
{txt}         25 {c |}{res}          2        9.52       95.24
{txt}         44 {c |}{res}          1        4.76      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}         21      100.00
{txt}
{com}. *There are only 21.
. list year sid sen dname dno geopost mmdpost dcand rcand ocand eseats if dontuse==0&dper==.&uncont==0
{txt}
        {c TLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 8}{c TRC}
        {c |} {res}year   sid   sen   dname   dno   geopost   mmdpost   dcand   rcand   ocand   eseats {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 8}{c RT}
  9482. {c |} {res}1971    17     0            25         .         .       1       1       0        1 {txt}{c |}
  9525. {c |} {res}1971    17     0            68         .         .       1       1       0        1 {txt}{c |}
  9526. {c |} {res}1971    17     0            69         .         .       1       1       0        1 {txt}{c |}
  9544. {c |} {res}1971    17     0            87         .         .       1       1       0        1 {txt}{c |}
 10091. {c |} {res}1972     4     1             3         .         .       1       1       0        1 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 8}{c RT}
 10094. {c |} {res}1972     4     1             6         .         .       1       1       0        1 {txt}{c |}
 10095. {c |} {res}1972     4     1             7         .         .       1       1       0        1 {txt}{c |}
 10097. {c |} {res}1972     4     1             9         .         .       1       1       0        1 {txt}{c |}
 10103. {c |} {res}1972     4     1            15         .         .       1       1       0        1 {txt}{c |}
 10104. {c |} {res}1972     4     1            16         .         .       1       1       0        1 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 8}{c RT}
 10108. {c |} {res}1972     4     1            20         .         .       1       1       0        1 {txt}{c |}
 10109. {c |} {res}1972     4     1            21         .         .       1       1       0        1 {txt}{c |}
 10117. {c |} {res}1972     4     1            29         .         .       1       1       0        1 {txt}{c |}
 10121. {c |} {res}1972     4     1            33         .         .       1       1       0        1 {txt}{c |}
 10122. {c |} {res}1972     4     1            34         .         .       1       1       0        1 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 8}{c RT}
 14119. {c |} {res}1972    44     0            26         .         .       1       1       0        1 {txt}{c |}
 38974. {c |} {res}1982    25     0            31         .         .       1       1       0        1 {txt}{c |}
 38975. {c |} {res}1982    25     0            32         .         .       1       1       0        1 {txt}{c |}
 41910. {c |} {res}1983    24     1            45         .         .       1       1       0        1 {txt}{c |}
 95388. {c |} {res}2002    11     0            28         .         .       1       1       0        1 {txt}{c |}
        {c LT}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 8}{c RT}
100363. {c |} {res}2004     4     0            65         .         .       1       1       0        1 {txt}{c |}
        {c BLC}{hline 6}{c -}{hline 5}{c -}{hline 5}{c -}{hline 7}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 7}{c -}{hline 7}{c -}{hline 7}{c -}{hline 8}{c BRC}

{com}. *Those should all be dontuse=1, and they're all clearly contested.  
. replace dontuse=1 if dper==.&uncont==0
{txt}(21 real changes made)

{com}. *21 changes with the above.  
. *make sure that there is no repub cand when dper=1 and no dem cand when rper=0
. assert dcand==0 if dper==0
{txt}
{com}. *always 0, good
. assert rcand==0 if dper==100
{txt}
{com}. *always 0, good
. sum sid if dontuse==0&votemiss==1&dper!=100&dper!=0&uncont==0

{txt}    Variable {c |}        Obs        Mean    Std. Dev.       Min        Max
{hline 13}{c +}{hline 57}
{space 9}sid {c |}{res}          0
{txt}
{com}. local aaa=r(N)
{txt}
{com}. assert `aaa'==0
{txt}
{com}. *always 0, good
. 
. *BIGTHIRD
. *Exclude cases with a strong non-major party presence.  
. *Scores of "1" indicate a large proportion of third party votes.
. *Scores of "2" track elections with 1) no dem & no repub cands, 2) at least one nonmaj winner, 3) at least one nonmaj inc, 4) at least one nonmaj legislator from the other chamber, or 5) at least one nonmaj legislator who served in the past.  
. gen bigthird=0
{txt}
{com}. gen oper=(ovote*eseats)/(dvote+rvote+ovote)
{txt}(5,575 missing values generated)

{com}. replace bigthird=1 if oper>.2&oper!=.&dcand==1&rcand==1
{txt}(645 real changes made)

{com}. drop oper
{txt}
{com}. replace bigthird=2 if (dcand==0&rcand==0)|(owin!=0&owin!=.)|(oinc!=0&oinc!=.)|(oother!=0&oother!=.)|(opast!=0&opast!=.)
{txt}(806 real changes made)

{com}. *CUMULATIVE CUTS
. replace cumulativecuts=cumulativecuts+1 if bigthird==1|bigthird==2
{txt}(1,270 real changes made)

{com}. tab cumulativecuts

      {txt}(max) {c |}
cumulativec {c |}
        uts {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}    135,031       96.29       96.29
{txt}          1 {c |}{res}      4,723        3.37       99.66
{txt}          2 {c |}{res}        457        0.33       99.98
{txt}          3 {c |}{res}         26        0.02      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}    140,237      100.00
{txt}
{com}. *CUMULATIVE CUTS: 3.72%
. 
. *D MINUS R
. *Compute as dif of dem and repub
. *Change to system missing when not to be used.
. foreach string in cand inc inc2 inc3 other leg2 leg3 past switch switchwin stealth stealthwin {c -(}
{txt}  2{com}. gen `string'=d`string'-r`string'
{txt}  3{com}. {c )-}
{txt}
{com}. 
. *PROPUP
. *Merge file that reports the proportion of seats up in each legislative chamber here.  
. merge m:1 year sid sen using tempchamberseats
{res}{txt}{p 0 7 2}
(note: variable
year was 
int, now double to accommodate using data's values)
{p_end}
{p 0 7 2}
(note: variable
sen was 
byte, now float to accommodate using data's values)
{p_end}

{col 5}Result{col 38}# of obs.
{col 5}{hline 41}
{col 5}not matched{col 30}{res}              58
{txt}{col 9}from master{col 30}{res}               2{txt}  (_merge==1)
{col 9}from using{col 30}{res}              56{txt}  (_merge==2)

{col 5}matched{col 30}{res}         140,235{txt}  (_merge==3)
{col 5}{hline 41}

{com}. erase tempchamberseats.dta
{txt}
{com}. *merge=1 only in the special elections in 2015 and 2017, if anything else is observed, there is a problem.  
. drop if _merge==2
{txt}(56 observations deleted)

{com}. assert _merge==3 if year!=2015&year!=2017
{txt}
{com}. drop _merge
{txt}
{com}. 
. 
. 
. *OVERVIEW: LAGGING VARIABLES
. *The general approach is to create a separate file for the lagged variables, and then replace year with year+termz, then merge these back into the main file.  
. *After the merge, lagged variables are changed to system missing if there has been redistricting.  
. *This is done (as opposed to the typical way of lagging variables) to solve the following problems.
. *1) It allows alternating seats to "leap-frog" over each other.
. *Alternating seats are in .59% of contests.  
. *CUMULATIVE CUTS
. replace cumulativecuts=cumulativecuts+1 if dtype>3&dtype<7
{txt}(922 real changes made)

{com}. tab cumulativecuts

      {txt}(max) {c |}
cumulativec {c |}
        uts {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}    134,147       95.66       95.66
{txt}          1 {c |}{res}      5,569        3.97       99.63
{txt}          2 {c |}{res}        495        0.35       99.98
{txt}          3 {c |}{res}         26        0.02      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}    140,237      100.00
{txt}
{com}. *CUMULATIVE CUTS: 4.34%
. *2) It allows districts that have changed their designations, but not their boundaries, to have lagged values.  .75% of contests.  
. *CUMULATIVE CUTS
. replace cumulativecuts=cumulativecuts+1 if redist==2|redist==7
{txt}(1,046 real changes made)

{com}. tab cumulativecuts

      {txt}(max) {c |}
cumulativec {c |}
        uts {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}    133,240       95.01       95.01
{txt}          1 {c |}{res}      6,345        4.52       99.54
{txt}          2 {c |}{res}        620        0.44       99.98
{txt}          3 {c |}{res}         30        0.02      100.00
{txt}          4 {c |}{res}          2        0.00      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}    140,237      100.00
{txt}
{com}. *CUMULATIVE CUTS: 4.99%
. *3) (Very minor) It allows incumbents who switch their posts in post-MMDs to have their lagged values associated with the other post.  
. *CUMULATIVE CUTS
. destring specpost, force gen(temp)
{txt}specpost: contains nonnumeric characters; temp {res}generated {txt}as {res}byte
{txt}(140186 missing values generated)
{res}{txt}
{com}. replace cumulativecuts=cumulativecuts+1 if temp!=.
{txt}(51 real changes made)

{com}. drop temp
{txt}
{com}. tab cumulativecuts

      {txt}(max) {c |}
cumulativec {c |}
        uts {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}    133,190       94.97       94.97
{txt}          1 {c |}{res}      6,394        4.56       99.53
{txt}          2 {c |}{res}        621        0.44       99.98
{txt}          3 {c |}{res}         30        0.02      100.00
{txt}          4 {c |}{res}          2        0.00      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}    140,237      100.00
{txt}
{com}. *CUMULATIVE CUTS: 5.02%
. *4) (Very minor) It allows FFA-MMDs that had some of their seats up two years ago, and some of their seats up four years ago, receive values from both of those contests to make its lagged values.  
. *CUMULATIVE CUTS: I'm not figuring this one out
. *5) (Very minor) Deals with unique problem for ID 1976
. *CUMULATIVE CUTS
. replace cumulativecuts=cumulativecuts+1 if sid==12&year==1976
{txt}(105 real changes made)

{com}. tab cumulativecuts

      {txt}(max) {c |}
cumulativec {c |}
        uts {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}    133,097       94.91       94.91
{txt}          1 {c |}{res}      6,475        4.62       99.53
{txt}          2 {c |}{res}        633        0.45       99.98
{txt}          3 {c |}{res}         30        0.02      100.00
{txt}          4 {c |}{res}          2        0.00      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}    140,237      100.00
{txt}
{com}. *CUMULATIVE CUTS: 5.09%
. drop cumulativecuts
{txt}
{com}. *6) It allows alternative lagged variables to be more easily utilized.
. *Alternative lagged variables are lagged values from 1) the other chamber when there is nesting or identical seats, 2) another post when there are post-mmds, or 3) lagged values two years ago when there are alternating seats in a district.  
. *7) Convenient way to generate cases out into the future for forecasting: generates a list of seats with their lagged values for (say) the 2018 election before it happens.  
. *8) It allows "lag aid" variables to be used to prevent many variables for many cases from being turned to system missing if there is redistricting for chambers with four year staggered terms.  
. *% of cases this would effect: would vary
. save $mainfile, replace
{txt}file 102slersuoacontest20181024.dta saved

{com}. 
. 
. 
. *LAGAIDVARS
. *Even with redistricting, a lagged variable doesn't need to be made sysmis for a case if there is no variation in that variable among the entire area that potentially could be in the contemporaneous district the case represents.  
. *Examples of potential variables.  
. *State or national level variables
. *other
. *past
. *With no additional geographic information beyond what state legislative district the locale is, this would mean that one could only know this if 1) all seats in a chamber were up last time, and there is no variation among those seats, or 2) not all seats were up last time, but between the second to last and last elections before redistricting there was no variation in the variable in question.  Don't do this if dontuselag is ever equal to "1" in the chamber in the period in question.  
. *Q: Say there is a senate with four year staggered terms.  Why is it necessary to see if there is no variation in BOTH 1988 and 1990, if the election in question is in 1994?  1988 and 1994 are six years apart?  
. *A: The reason is because of deferred voters.  A group of voters in one locale may vote for a state senator in 1988 and not get to vote for a state senator again until 1994.  
. *Q: And why do we need 1990 for the election up in 1992?
. *A: Because of accelerated voters.  Just because I voted for a state senator for a four year term in 1990 doesn't mean I don't get to vote for another state senator for a four year term in 1992.  
. *Most of the lagged variables will have variation, but each variable is tested for this property anyway to take advantage of the few times there isn't for the variables that show the most variation.  
. *Preliminary work: create two files to do this.
. *file #1 indicates whether there is any variation in the variables that will be lagged in chambers where all the seats are up.  
. *file #2 indicates whether there is any variation in the variables that will be lagged among chambers that aren't all up in two election years in a row, but only if no redistricting occurred in the second election year in question.  These will then be matched with elections two and four years in the future (but only if no non-holdover redistricting occurred in the second year).  If a seat is only up twice in the earlier time period range, only keep the second election.  Moving bands four years wide will be used.  (The problem with using three year time ranges is the KY Senate elections when there were briefly five year terms.)
. *GLOBAL
. *The following are the variables that will be in the lagaid files.  
. global lagaidvars dper cand inc inc2 inc3 other leg2 leg3 past switch switchwin stealth stealthwin partuncont mixeduncont uncont dwin bigthird dontuse
{txt}
{com}. *PROGRAM
. capture program drop computelagaidvars
{txt}
{com}. program define computelagaidvars
{txt}  1{com}. *Collapse to uoa=chamber-year, and compute relevant vars.  
. foreach string2 in novar mean miss {c -(}
{txt}  2{com}. *DEPTH=1
. local lagaid`string2' ""
{txt}  3{com}. foreach string of global lagaidvars {c -(}
{txt}  4{com}. *DEPTH=2
. gen `string'`string2'=`string'
{txt}  5{com}. local lagaid`string2' `lagaid`string2'' `string'`string2'
{txt}  6{com}. {c )-}
{txt}  7{com}. *DEPTH=1
. {c )-}
{txt}  8{com}. foreach string of local lagaidmiss {c -(}
{txt}  9{com}. *DEPTH=2
. replace `string'=`string'==.
{txt} 10{com}. {c )-}
{txt} 11{com}. *DEPTH=1
. collapse (sum) eseats (sd) `lagaidnovar' (mean) `lagaidmean' totalseats (max) `lagaidmiss', by(yearlagaid sid sen)
{txt} 12{com}. foreach string of global lagaidvars {c -(}
{txt} 13{com}. *DEPTH=2
. replace `string'novar=`string'novar==0
{txt} 14{com}. replace `string'novar=0 if `string'miss==1
{txt} 15{com}. drop `string'miss
{txt} 16{com}. {c )-}
{txt} 17{com}. *DEPTH=1
. end
{txt}
{com}. 
. *FILE #1
. *Get SD of variable values within a year group.  Also get the mean of all those when there is no variation and pair them up.  Track missing values of anything, and make novar1 variables "0" if there are any missing cases for a variable.
. clear
{txt}
{com}. use $mainfile
{txt}
{com}. rename year yearlagaid
{res}{txt}
{com}. egen max=max(dontuse), by(yearlagaid sid sen)
{txt}
{com}. keep if propup==1|max==0
{txt}(397 observations deleted)

{com}. *Program
. computelagaidvars
{txt}(229 missing values generated)
(229 missing values generated)
(229 missing values generated)
(121,002 real changes made)
(53,173 real changes made)
(108,583 real changes made)
(41,810 real changes made)
(42,943 real changes made)
(4,051 real changes made)
(41,520 real changes made)
(47,443 real changes made)
(5,881 real changes made)
(769 real changes made)
(592 real changes made)
(553 real changes made)
(292 real changes made)
(2,887 real changes made)
(228 real changes made)
(50,847 real changes made)
(79,374 real changes made)
(1,265 real changes made)
(500 real changes made)
(2,218 real changes made)
(1 real change made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)
(2,218 real changes made)
(0 real changes made)

{com}. drop if eseats!=totalseats
{txt}(626 observations deleted)

{com}. drop eseats totalseats
{txt}
{com}. save templagaid1, replace
{txt}(note: file templagaid1.dta not found)
file templagaid1.dta saved

{com}. 
. *File #2
. *Four smaller files will be made to construct this file.  One will represent presidential years, the second prez year+1, the third prez year+2 and the fourth prez year+3.
. clear
{txt}
{com}. use $mainfile
{txt}
{com}. drop in 1/l
{txt}(140,237 observations deleted)

{com}. save templagaid2, replace
{txt}(note: dataset contains 0 observations)
(note: file templagaid2.dta not found)
file templagaid2.dta saved

{com}. forvalues aaa=0/3 {c -(}
{txt}  2{com}. clear
{txt}  3{com}. use $mainfile
{txt}  4{com}. gen quad=mod(year+`aaa',4)
{txt}  5{com}. gen yearnum=quad if quad>0
{txt}  6{com}. replace yearnum=4 if quad==0
{txt}  7{com}. drop quad
{txt}  8{com}. gen yearlagaid=year+(4-yearnum)
{txt}  9{com}. di "`aaa'"
{txt} 10{com}. tab year yearnum
{txt} 11{com}. append using templagaid2
{txt} 12{com}. save templagaid2, replace
{txt} 13{com}. {c )-}
{txt}(68,082 missing values generated)
(68,082 real changes made)
0

           {c |}                   yearnum
      year {c |}         1          2          3          4 {c |}     Total
{hline 11}{c +}{hline 44}{c +}{hline 10}
      1968 {c |}{res}         0          0          0      4,463 {txt}{c |}{res}     4,463 
{txt}      1969 {c |}{res}       222          0          0          0 {txt}{c |}{res}       222 
{txt}      1970 {c |}{res}         0      4,772          0          0 {txt}{c |}{res}     4,772 
{txt}      1971 {c |}{res}         0          0        444          0 {txt}{c |}{res}       444 
{txt}      1972 {c |}{res}         0          0          0      4,638 {txt}{c |}{res}     4,638 
{txt}      1973 {c |}{res}       252          0          0          0 {txt}{c |}{res}       252 
{txt}      1974 {c |}{res}         0      5,058          0          0 {txt}{c |}{res}     5,058 
{txt}      1975 {c |}{res}         0          0        423          0 {txt}{c |}{res}       423 
{txt}      1976 {c |}{res}         0          0          0      4,999 {txt}{c |}{res}     4,999 
{txt}      1977 {c |}{res}       251          0          0          0 {txt}{c |}{res}       251 
{txt}      1978 {c |}{res}         0      5,058          0          0 {txt}{c |}{res}     5,058 
{txt}      1979 {c |}{res}         0          0        425          0 {txt}{c |}{res}       425 
{txt}      1980 {c |}{res}         0          0          0      4,924 {txt}{c |}{res}     4,924 
{txt}      1981 {c |}{res}       250          0          0          0 {txt}{c |}{res}       250 
{txt}      1982 {c |}{res}         0      5,405          0          0 {txt}{c |}{res}     5,405 
{txt}      1983 {c |}{res}         0          0        603          0 {txt}{c |}{res}       603 
{txt}      1984 {c |}{res}         0          0          0      5,179 {txt}{c |}{res}     5,179 
{txt}      1985 {c |}{res}       140          0          0          0 {txt}{c |}{res}       140 
{txt}      1986 {c |}{res}         0      5,570          0          0 {txt}{c |}{res}     5,570 
{txt}      1987 {c |}{res}         0          0        394          0 {txt}{c |}{res}       394 
{txt}      1988 {c |}{res}         0          0          0      5,335 {txt}{c |}{res}     5,335 
{txt}      1989 {c |}{res}       142          0          0          0 {txt}{c |}{res}       142 
{txt}      1990 {c |}{res}         0      5,541          0          0 {txt}{c |}{res}     5,541 
{txt}      1991 {c |}{res}         0          0        394          0 {txt}{c |}{res}       394 
{txt}      1992 {c |}{res}         0          0          0      5,804 {txt}{c |}{res}     5,804 
{txt}      1993 {c |}{res}       180          0          0          0 {txt}{c |}{res}       180 
{txt}      1994 {c |}{res}         0      5,614          0          0 {txt}{c |}{res}     5,614 
{txt}      1995 {c |}{res}         0          0        355          0 {txt}{c |}{res}       355 
{txt}      1996 {c |}{res}         0          0          0      5,508 {txt}{c |}{res}     5,508 
{txt}      1997 {c |}{res}       180          0          0          0 {txt}{c |}{res}       180 
{txt}      1998 {c |}{res}         0      5,614          0          0 {txt}{c |}{res}     5,614 
{txt}      1999 {c |}{res}         0          0        355          0 {txt}{c |}{res}       355 
{txt}      2000 {c |}{res}         0          0          0      5,469 {txt}{c |}{res}     5,469 
{txt}      2001 {c |}{res}       180          0          0          0 {txt}{c |}{res}       180 
{txt}      2002 {c |}{res}         0      5,652          0          0 {txt}{c |}{res}     5,652 
{txt}      2003 {c |}{res}         0          0        394          0 {txt}{c |}{res}       394 
{txt}      2004 {c |}{res}         0          0          0      5,311 {txt}{c |}{res}     5,311 
{txt}      2005 {c |}{res}       140          0          0          0 {txt}{c |}{res}       140 
{txt}      2006 {c |}{res}         0      5,552          0          0 {txt}{c |}{res}     5,552 
{txt}      2007 {c |}{res}         0          0        394          0 {txt}{c |}{res}       394 
{txt}      2008 {c |}{res}         0          0          0      5,323 {txt}{c |}{res}     5,323 
{txt}      2009 {c |}{res}       142          0          0          0 {txt}{c |}{res}       142 
{txt}      2010 {c |}{res}         0      5,547          0          0 {txt}{c |}{res}     5,547 
{txt}      2011 {c |}{res}         0          0        394          0 {txt}{c |}{res}       394 
{txt}      2012 {c |}{res}         0          0          0      5,610 {txt}{c |}{res}     5,610 
{txt}      2013 {c |}{res}       180          0          0          0 {txt}{c |}{res}       180 
{txt}      2014 {c |}{res}         0      5,581          0          0 {txt}{c |}{res}     5,581 
{txt}      2015 {c |}{res}         0          0        356          0 {txt}{c |}{res}       356 
{txt}      2016 {c |}{res}         0          0          0      5,519 {txt}{c |}{res}     5,519 
{txt}      2017 {c |}{res}         1          0          0          0 {txt}{c |}{res}         1 
{txt}{hline 11}{c +}{hline 44}{c +}{hline 10}
     Total {c |}{res}     2,260     64,964      4,931     68,082 {txt}{c |}{res}   140,237 
{txt}file templagaid2.dta saved
(4,931 missing values generated)
(4,931 real changes made)
1

           {c |}                   yearnum
      year {c |}         1          2          3          4 {c |}     Total
{hline 11}{c +}{hline 44}{c +}{hline 10}
      1968 {c |}{res}     4,463          0          0          0 {txt}{c |}{res}     4,463 
{txt}      1969 {c |}{res}         0        222          0          0 {txt}{c |}{res}       222 
{txt}      1970 {c |}{res}         0          0      4,772          0 {txt}{c |}{res}     4,772 
{txt}      1971 {c |}{res}         0          0          0        444 {txt}{c |}{res}       444 
{txt}      1972 {c |}{res}     4,638          0          0          0 {txt}{c |}{res}     4,638 
{txt}      1973 {c |}{res}         0        252          0          0 {txt}{c |}{res}       252 
{txt}      1974 {c |}{res}         0          0      5,058          0 {txt}{c |}{res}     5,058 
{txt}      1975 {c |}{res}         0          0          0        423 {txt}{c |}{res}       423 
{txt}      1976 {c |}{res}     4,999          0          0          0 {txt}{c |}{res}     4,999 
{txt}      1977 {c |}{res}         0        251          0          0 {txt}{c |}{res}       251 
{txt}      1978 {c |}{res}         0          0      5,058          0 {txt}{c |}{res}     5,058 
{txt}      1979 {c |}{res}         0          0          0        425 {txt}{c |}{res}       425 
{txt}      1980 {c |}{res}     4,924          0          0          0 {txt}{c |}{res}     4,924 
{txt}      1981 {c |}{res}         0        250          0          0 {txt}{c |}{res}       250 
{txt}      1982 {c |}{res}         0          0      5,405          0 {txt}{c |}{res}     5,405 
{txt}      1983 {c |}{res}         0          0          0        603 {txt}{c |}{res}       603 
{txt}      1984 {c |}{res}     5,179          0          0          0 {txt}{c |}{res}     5,179 
{txt}      1985 {c |}{res}         0        140          0          0 {txt}{c |}{res}       140 
{txt}      1986 {c |}{res}         0          0      5,570          0 {txt}{c |}{res}     5,570 
{txt}      1987 {c |}{res}         0          0          0        394 {txt}{c |}{res}       394 
{txt}      1988 {c |}{res}     5,335          0          0          0 {txt}{c |}{res}     5,335 
{txt}      1989 {c |}{res}         0        142          0          0 {txt}{c |}{res}       142 
{txt}      1990 {c |}{res}         0          0      5,541          0 {txt}{c |}{res}     5,541 
{txt}      1991 {c |}{res}         0          0          0        394 {txt}{c |}{res}       394 
{txt}      1992 {c |}{res}     5,804          0          0          0 {txt}{c |}{res}     5,804 
{txt}      1993 {c |}{res}         0        180          0          0 {txt}{c |}{res}       180 
{txt}      1994 {c |}{res}         0          0      5,614          0 {txt}{c |}{res}     5,614 
{txt}      1995 {c |}{res}         0          0          0        355 {txt}{c |}{res}       355 
{txt}      1996 {c |}{res}     5,508          0          0          0 {txt}{c |}{res}     5,508 
{txt}      1997 {c |}{res}         0        180          0          0 {txt}{c |}{res}       180 
{txt}      1998 {c |}{res}         0          0      5,614          0 {txt}{c |}{res}     5,614 
{txt}      1999 {c |}{res}         0          0          0        355 {txt}{c |}{res}       355 
{txt}      2000 {c |}{res}     5,469          0          0          0 {txt}{c |}{res}     5,469 
{txt}      2001 {c |}{res}         0        180          0          0 {txt}{c |}{res}       180 
{txt}      2002 {c |}{res}         0          0      5,652          0 {txt}{c |}{res}     5,652 
{txt}      2003 {c |}{res}         0          0          0        394 {txt}{c |}{res}       394 
{txt}      2004 {c |}{res}     5,311          0          0          0 {txt}{c |}{res}     5,311 
{txt}      2005 {c |}{res}         0        140          0          0 {txt}{c |}{res}       140 
{txt}      2006 {c |}{res}         0          0      5,552          0 {txt}{c |}{res}     5,552 
{txt}      2007 {c |}{res}         0          0          0        394 {txt}{c |}{res}       394 
{txt}      2008 {c |}{res}     5,323          0          0          0 {txt}{c |}{res}     5,323 
{txt}      2009 {c |}{res}         0        142          0          0 {txt}{c |}{res}       142 
{txt}      2010 {c |}{res}         0          0      5,547          0 {txt}{c |}{res}     5,547 
{txt}      2011 {c |}{res}         0          0          0        394 {txt}{c |}{res}       394 
{txt}      2012 {c |}{res}     5,610          0          0          0 {txt}{c |}{res}     5,610 
{txt}      2013 {c |}{res}         0        180          0          0 {txt}{c |}{res}       180 
{txt}      2014 {c |}{res}         0          0      5,581          0 {txt}{c |}{res}     5,581 
{txt}      2015 {c |}{res}         0          0          0        356 {txt}{c |}{res}       356 
{txt}      2016 {c |}{res}     5,519          0          0          0 {txt}{c |}{res}     5,519 
{txt}      2017 {c |}{res}         0          1          0          0 {txt}{c |}{res}         1 
{txt}{hline 11}{c +}{hline 44}{c +}{hline 10}
     Total {c |}{res}    68,082      2,260     64,964      4,931 {txt}{c |}{res}   140,237 
{txt}file templagaid2.dta saved
(64,964 missing values generated)
(64,964 real changes made)
2

           {c |}                   yearnum
      year {c |}         1          2          3          4 {c |}     Total
{hline 11}{c +}{hline 44}{c +}{hline 10}
      1968 {c |}{res}         0      4,463          0          0 {txt}{c |}{res}     4,463 
{txt}      1969 {c |}{res}         0          0        222          0 {txt}{c |}{res}       222 
{txt}      1970 {c |}{res}         0          0          0      4,772 {txt}{c |}{res}     4,772 
{txt}      1971 {c |}{res}       444          0          0          0 {txt}{c |}{res}       444 
{txt}      1972 {c |}{res}         0      4,638          0          0 {txt}{c |}{res}     4,638 
{txt}      1973 {c |}{res}         0          0        252          0 {txt}{c |}{res}       252 
{txt}      1974 {c |}{res}         0          0          0      5,058 {txt}{c |}{res}     5,058 
{txt}      1975 {c |}{res}       423          0          0          0 {txt}{c |}{res}       423 
{txt}      1976 {c |}{res}         0      4,999          0          0 {txt}{c |}{res}     4,999 
{txt}      1977 {c |}{res}         0          0        251          0 {txt}{c |}{res}       251 
{txt}      1978 {c |}{res}         0          0          0      5,058 {txt}{c |}{res}     5,058 
{txt}      1979 {c |}{res}       425          0          0          0 {txt}{c |}{res}       425 
{txt}      1980 {c |}{res}         0      4,924          0          0 {txt}{c |}{res}     4,924 
{txt}      1981 {c |}{res}         0          0        250          0 {txt}{c |}{res}       250 
{txt}      1982 {c |}{res}         0          0          0      5,405 {txt}{c |}{res}     5,405 
{txt}      1983 {c |}{res}       603          0          0          0 {txt}{c |}{res}       603 
{txt}      1984 {c |}{res}         0      5,179          0          0 {txt}{c |}{res}     5,179 
{txt}      1985 {c |}{res}         0          0        140          0 {txt}{c |}{res}       140 
{txt}      1986 {c |}{res}         0          0          0      5,570 {txt}{c |}{res}     5,570 
{txt}      1987 {c |}{res}       394          0          0          0 {txt}{c |}{res}       394 
{txt}      1988 {c |}{res}         0      5,335          0          0 {txt}{c |}{res}     5,335 
{txt}      1989 {c |}{res}         0          0        142          0 {txt}{c |}{res}       142 
{txt}      1990 {c |}{res}         0          0          0      5,541 {txt}{c |}{res}     5,541 
{txt}      1991 {c |}{res}       394          0          0          0 {txt}{c |}{res}       394 
{txt}      1992 {c |}{res}         0      5,804          0          0 {txt}{c |}{res}     5,804 
{txt}      1993 {c |}{res}         0          0        180          0 {txt}{c |}{res}       180 
{txt}      1994 {c |}{res}         0          0          0      5,614 {txt}{c |}{res}     5,614 
{txt}      1995 {c |}{res}       355          0          0          0 {txt}{c |}{res}       355 
{txt}      1996 {c |}{res}         0      5,508          0          0 {txt}{c |}{res}     5,508 
{txt}      1997 {c |}{res}         0          0        180          0 {txt}{c |}{res}       180 
{txt}      1998 {c |}{res}         0          0          0      5,614 {txt}{c |}{res}     5,614 
{txt}      1999 {c |}{res}       355          0          0          0 {txt}{c |}{res}       355 
{txt}      2000 {c |}{res}         0      5,469          0          0 {txt}{c |}{res}     5,469 
{txt}      2001 {c |}{res}         0          0        180          0 {txt}{c |}{res}       180 
{txt}      2002 {c |}{res}         0          0          0      5,652 {txt}{c |}{res}     5,652 
{txt}      2003 {c |}{res}       394          0          0          0 {txt}{c |}{res}       394 
{txt}      2004 {c |}{res}         0      5,311          0          0 {txt}{c |}{res}     5,311 
{txt}      2005 {c |}{res}         0          0        140          0 {txt}{c |}{res}       140 
{txt}      2006 {c |}{res}         0          0          0      5,552 {txt}{c |}{res}     5,552 
{txt}      2007 {c |}{res}       394          0          0          0 {txt}{c |}{res}       394 
{txt}      2008 {c |}{res}         0      5,323          0          0 {txt}{c |}{res}     5,323 
{txt}      2009 {c |}{res}         0          0        142          0 {txt}{c |}{res}       142 
{txt}      2010 {c |}{res}         0          0          0      5,547 {txt}{c |}{res}     5,547 
{txt}      2011 {c |}{res}       394          0          0          0 {txt}{c |}{res}       394 
{txt}      2012 {c |}{res}         0      5,610          0          0 {txt}{c |}{res}     5,610 
{txt}      2013 {c |}{res}         0          0        180          0 {txt}{c |}{res}       180 
{txt}      2014 {c |}{res}         0          0          0      5,581 {txt}{c |}{res}     5,581 
{txt}      2015 {c |}{res}       356          0          0          0 {txt}{c |}{res}       356 
{txt}      2016 {c |}{res}         0      5,519          0          0 {txt}{c |}{res}     5,519 
{txt}      2017 {c |}{res}         0          0          1          0 {txt}{c |}{res}         1 
{txt}{hline 11}{c +}{hline 44}{c +}{hline 10}
     Total {c |}{res}     4,931     68,082      2,260     64,964 {txt}{c |}{res}   140,237 
{txt}file templagaid2.dta saved
(2,260 missing values generated)
(2,260 real changes made)
3

           {c |}                   yearnum
      year {c |}         1          2          3          4 {c |}     Total
{hline 11}{c +}{hline 44}{c +}{hline 10}
      1968 {c |}{res}         0          0      4,463          0 {txt}{c |}{res}     4,463 
{txt}      1969 {c |}{res}         0          0          0        222 {txt}{c |}{res}       222 
{txt}      1970 {c |}{res}     4,772          0          0          0 {txt}{c |}{res}     4,772 
{txt}      1971 {c |}{res}         0        444          0          0 {txt}{c |}{res}       444 
{txt}      1972 {c |}{res}         0          0      4,638          0 {txt}{c |}{res}     4,638 
{txt}      1973 {c |}{res}         0          0          0        252 {txt}{c |}{res}       252 
{txt}      1974 {c |}{res}     5,058          0          0          0 {txt}{c |}{res}     5,058 
{txt}      1975 {c |}{res}         0        423          0          0 {txt}{c |}{res}       423 
{txt}      1976 {c |}{res}         0          0      4,999          0 {txt}{c |}{res}     4,999 
{txt}      1977 {c |}{res}         0          0          0        251 {txt}{c |}{res}       251 
{txt}      1978 {c |}{res}     5,058          0          0          0 {txt}{c |}{res}     5,058 
{txt}      1979 {c |}{res}         0        425          0          0 {txt}{c |}{res}       425 
{txt}      1980 {c |}{res}         0          0      4,924          0 {txt}{c |}{res}     4,924 
{txt}      1981 {c |}{res}         0          0          0        250 {txt}{c |}{res}       250 
{txt}      1982 {c |}{res}     5,405          0          0          0 {txt}{c |}{res}     5,405 
{txt}      1983 {c |}{res}         0        603          0          0 {txt}{c |}{res}       603 
{txt}      1984 {c |}{res}         0          0      5,179          0 {txt}{c |}{res}     5,179 
{txt}      1985 {c |}{res}         0          0          0        140 {txt}{c |}{res}       140 
{txt}      1986 {c |}{res}     5,570          0          0          0 {txt}{c |}{res}     5,570 
{txt}      1987 {c |}{res}         0        394          0          0 {txt}{c |}{res}       394 
{txt}      1988 {c |}{res}         0          0      5,335          0 {txt}{c |}{res}     5,335 
{txt}      1989 {c |}{res}         0          0          0        142 {txt}{c |}{res}       142 
{txt}      1990 {c |}{res}     5,541          0          0          0 {txt}{c |}{res}     5,541 
{txt}      1991 {c |}{res}         0        394          0          0 {txt}{c |}{res}       394 
{txt}      1992 {c |}{res}         0          0      5,804          0 {txt}{c |}{res}     5,804 
{txt}      1993 {c |}{res}         0          0          0        180 {txt}{c |}{res}       180 
{txt}      1994 {c |}{res}     5,614          0          0          0 {txt}{c |}{res}     5,614 
{txt}      1995 {c |}{res}         0        355          0          0 {txt}{c |}{res}       355 
{txt}      1996 {c |}{res}         0          0      5,508          0 {txt}{c |}{res}     5,508 
{txt}      1997 {c |}{res}         0          0          0        180 {txt}{c |}{res}       180 
{txt}      1998 {c |}{res}     5,614          0          0          0 {txt}{c |}{res}     5,614 
{txt}      1999 {c |}{res}         0        355          0          0 {txt}{c |}{res}       355 
{txt}      2000 {c |}{res}         0          0      5,469          0 {txt}{c |}{res}     5,469 
{txt}      2001 {c |}{res}         0          0          0        180 {txt}{c |}{res}       180 
{txt}      2002 {c |}{res}     5,652          0          0          0 {txt}{c |}{res}     5,652 
{txt}      2003 {c |}{res}         0        394          0          0 {txt}{c |}{res}       394 
{txt}      2004 {c |}{res}         0          0      5,311          0 {txt}{c |}{res}     5,311 
{txt}      2005 {c |}{res}         0          0          0        140 {txt}{c |}{res}       140 
{txt}      2006 {c |}{res}     5,552          0          0          0 {txt}{c |}{res}     5,552 
{txt}      2007 {c |}{res}         0        394          0          0 {txt}{c |}{res}       394 
{txt}      2008 {c |}{res}         0          0      5,323          0 {txt}{c |}{res}     5,323 
{txt}      2009 {c |}{res}         0          0          0        142 {txt}{c |}{res}       142 
{txt}      2010 {c |}{res}     5,547          0          0          0 {txt}{c |}{res}     5,547 
{txt}      2011 {c |}{res}         0        394          0          0 {txt}{c |}{res}       394 
{txt}      2012 {c |}{res}         0          0      5,610          0 {txt}{c |}{res}     5,610 
{txt}      2013 {c |}{res}         0          0          0        180 {txt}{c |}{res}       180 
{txt}      2014 {c |}{res}     5,581          0          0          0 {txt}{c |}{res}     5,581 
{txt}      2015 {c |}{res}         0        356          0          0 {txt}{c |}{res}       356 
{txt}      2016 {c |}{res}         0          0      5,519          0 {txt}{c |}{res}     5,519 
{txt}      2017 {c |}{res}         0          0          0          1 {txt}{c |}{res}         1 
{txt}{hline 11}{c +}{hline 44}{c +}{hline 10}
     Total {c |}{res}    64,964      4,931     68,082      2,260 {txt}{c |}{res}   140,237 
{txt}file templagaid2.dta saved

{com}. *drop earlier years within yearlagaid2 group, if two or more election years appear.  
. egen max=max(year), by(sid sen dname dno geopost mmdpost yearlagaid)
{txt}
{com}. *Dropping redist=1 cases is okay because its only when all seats in a chamber are accounted for and the two election years necessary to account for them all aren't divided by redistricting that any of them are used.  
. *However, don't drop a district if it was redistricted within the first two years of a four year group.  Say that the group includes 1987 to 1990.  A district was up in 1988 and was redist=1.  The district wasn't up in 1990.  Then there was redistricting again in 1992.  The way it is now, 1988 wouldn't be used, and it should be used.  
. drop if (year!=max)|(redist!=0&yearnum>2)
{txt}(301,117 observations deleted)

{com}. drop max
{txt}
{com}. egen max=max(dontuse), by(yearlagaid sid sen)
{txt}
{com}. drop if max==1
{txt}(21,889 observations deleted)

{com}. *get rid of WV Sen cases, these will be dealt with below.  
. drop if sid==48&sen==1
{txt}(1,088 observations deleted)

{com}. *Program
. computelagaidvars
{txt}(111 missing values generated)
(111 missing values generated)
(111 missing values generated)
(203,363 real changes made)
(89,046 real changes made)
(185,665 real changes made)
(74,262 real changes made)
(73,783 real changes made)
(8,973 real changes made)
(72,914 real changes made)
(84,312 real changes made)
(10,446 real changes made)
(1,363 real changes made)
(1,030 real changes made)
(832 real changes made)
(442 real changes made)
(4,466 real changes made)
(380 real changes made)
(85,093 real changes made)
(131,375 real changes made)
(1,940 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)
(3,857 real changes made)
(0 real changes made)

{com}. drop if eseats!=totalseats
{txt}(754 observations deleted)

{com}. drop eseats totalseats
{txt}
{com}. save templagaid2, replace
{txt}file templagaid2.dta saved

{com}. 
. *File #3: WVSEN
. *Add the WV Senate, except that instead of making sure that all seats were up in the past, the entire state only needs to be covered with mmdpost=1 or mmdpost=2.  If there are etype=gs elections, those can be two years prior to the contemporaneous election instead of four years prior.  
. *The WV Senate is unique in that other state senates with four year term alternating seats do not cover the entire state.
. clear
{txt}
{com}. use $mainfile
{txt}
{com}. keep if sid==48&sen==1
{txt}(139,804 observations deleted)

{com}. gen quad=mod(year,4)
{txt}
{com}. *postmmd=1 (& 3) are generally up in presidential years while postmmd=2 (& 4) are generally up in midterm years.  
. gen yearlagaid=year if quad==0&(mmdpost==1|mmdpost==3)
{txt}(212 missing values generated)

{com}. replace yearlagaid=year-2 if quad==2&(mmdpost==1|mmdpost==3)&yearlagaid==.
{txt}(3 real changes made)

{com}. replace yearlagaid=year if quad==2&(mmdpost==2|mmdpost==4)&yearlagaid==.
{txt}(204 real changes made)

{com}. replace yearlagaid=year-2 if quad==0&(mmdpost==2|mmdpost==4)&yearlagaid==.
{txt}(5 real changes made)

{com}. *drop earlier years within yearlagaid group, if two or more elections years appear.  
. egen max=max(year), by(dno mmdpost yearlagaid)
{txt}
{com}. drop if (year!=max)|[redist!=0&quad==2&(mmdpost==1|mmdpost==3)]|[redist!=0&quad==2&(mmdpost==2|mmdpost==4)]
{txt}(91 observations deleted)

{com}. drop max
{txt}
{com}. egen max=max(dontuse), by(yearlagaid sid sen)
{txt}
{com}. drop if max==1
{txt}(68 observations deleted)

{com}. *Program
. computelagaidvars
{txt}(266 real changes made)
(76 real changes made)
(183 real changes made)
(80 real changes made)
(96 real changes made)
(45 real changes made)
(85 real changes made)
(119 real changes made)
(34 real changes made)
(1 real change made)
(1 real change made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(76 real changes made)
(208 real changes made)
(0 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)
(18 real changes made)
(0 real changes made)

{com}. *17 seats have to be accounted for, as there are 34 seats in the WV state senate.  
. drop if eseats!=17
{txt}(2 observations deleted)

{com}. drop eseats totalseats
{txt}
{com}. save templagaid3, replace
{txt}(note: file templagaid3.dta not found)
file templagaid3.dta saved

{com}. 
. 
. 
. 
. *LAGVARs
. *Create main file with lagged vars to be merged into the main file.  
. *SPLIT
. *split lagged file into two files, one with the max weights, one with the min weights.  
. *inc2 and inc3 aren't lagged, since they are almost perfectly collinear with inc and inclag when both of the latter are included in a model.  
. clear
{txt}
{com}. use $mainfile
{txt}
{com}. *The parts of specpost that are alpha shouldn't be put into mmdpost in the following.  
. destring specpost, force gen(temp)
{txt}specpost: contains nonnumeric characters; temp {res}generated {txt}as {res}byte
{txt}(140186 missing values generated)
{res}{txt}
{com}. replace mmdpost=temp if temp!=.
{txt}(51 real changes made)

{com}. drop temp
{txt}
{com}. keep year sid sen dname dno geopost mmdpost maxweight minweight maxtermz mintermz dper dvote rvote ovote cand inc inc2 inc3 other leg2 leg3 past switch switchwin stealth stealthwin bigthird dontuse propup partuncont mixeduncont uncont redist dwin
{txt}
{com}. save tempslerslagged, replace
{txt}(note: file tempslerslagged.dta not found)
file tempslerslagged.dta saved

{com}. *LAGFILE1
. *Create temp lag file #1, with maxtermz
. clear
{txt}
{com}. use tempslerslagged
{txt}
{com}. drop minweight mintermz
{txt}
{com}. gen weight=maxweight
{txt}
{com}. rename maxtermz termz
{res}{txt}
{com}. *Break id 1974 house off and give it posts.  Give it posts here first.  
. replace mmdpost=1 if sid==12&sen==0&year==1974
{txt}(35 real changes made)

{com}. save temp, replace
{txt}(note: file temp.dta not found)
file temp.dta saved

{com}. keep if sid==12&sen==0&year==1974
{txt}(140,202 observations deleted)

{com}. replace mmdpost=2
{txt}(35 real changes made)

{com}. append using temp
{txt}
{com}. save temp, replace
{txt}file temp.dta saved

{com}. *LAGFILE2
. *Create temp lag file #2, with mintermz
. *id 1974 house doesn't have to be messed with with this one
. clear
{txt}
{com}. use tempslerslagged
{txt}
{com}. drop maxweight maxtermz
{txt}
{com}. gen weight=minweight
{txt}
{com}. rename mintermz termz
{res}{txt}
{com}. drop if weight==0
{txt}(140,219 observations deleted)

{com}. *APPEND
. *Put the two lag files together
. append using temp
{txt}
{com}. erase temp.dta
{txt}
{com}. *YEAR
. rename year yearlag
{res}{txt}
{com}. gen year=yearlag+termz
{txt}
{com}. *In one situation, a district is up for an election in a year ending in "0", and has a four year term.  Two years later, in a year ending in "2", which is a redistricting year, a district with the same number is up for election, and has a two year term.  One inappropriate modeling strategy would result in the district up in the year ending in "0" contributing to the lagged value of the election up in the year ending in "4."  So when more than one election is nested in an election that is going to have a lagged value, and the later one of those elections (in this case, the one taking place in a year ending in "2") has redist!=0, drop the lagged values of the earlier election.  
. *Another situation is similar to the above, but is more problematic.  In this example, a district is up for an election in a year ending in "0" and has a four year term.  Two years later, the map is redrawn.  There is no election for a district with that name/number in a year ending in "2."  Two years after that, there is re-redistricting, and the district in question has a value of redist=2.  I don't see the problem.  If redist=1, then no lagged value will arrive there.  If redist=2, it should be in terms of the map that was in place the last time an election in that locale was conducted, which, by the definition of this example, it wasn't in a year ending in "2."  What if the term for the prior district in question was only two years, but it was four years ago when it was up?  Is it possible for a situation to be that unfair/messed up?  Anything is possible.  
. bysort year sid sen dname dno geopost mmdpost (yearlag): gen row=_n
{txt}
{com}. tab row

        {txt}row {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          1 {c |}{res}    140,251       99.97       99.97
{txt}          2 {c |}{res}         39        0.03      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}    140,290      100.00
{txt}
{com}. *There are either values of 1 or 2.  There are 39 cases of row=2.  
. gen temp=row==2&redist==1
{txt}
{com}. tab temp

       {txt}temp {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}    140,260       99.98       99.98
{txt}          1 {c |}{res}         30        0.02      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}    140,290      100.00
{txt}
{com}. *there are 30 cases of temp=1
. tab sid sen if temp==1

 {txt}number of {c |}
  state in {c |}        senate
alpha list {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
         2 {c |}{res}         0          4 {txt}{c |}{res}         4 
{txt}         6 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}        11 {c |}{res}         0          2 {txt}{c |}{res}         2 
{txt}        15 {c |}{res}         0         18 {txt}{c |}{res}        18 
{txt}        26 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}        34 {c |}{res}         1          1 {txt}{c |}{res}         2 
{txt}        37 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}        44 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}         1         29 {txt}{c |}{res}        30 
{txt}
{com}. egen redistproblem=max(temp), by(year sid sen dname dno geopost mmdpost)
{txt}
{com}. tab year sid if redistproblem==1

           {txt}{c |}                              number of state in alpha list
      year {c |}         2          6         11         15         26         34         37         44 {c |}     Total
{hline 11}{c +}{hline 88}{c +}{hline 10}
      1972 {c |}{res}         0          0          0         12          0          0          0          0 {txt}{c |}{res}        12 
{txt}      1976 {c |}{res}         4          0          0          0          0          0          0          0 {txt}{c |}{res}         4 
{txt}      1984 {c |}{res}         0          2          2          6          0          0          0          0 {txt}{c |}{res}        10 
{txt}      1986 {c |}{res}         2          0          0          0          0          0          0          0 {txt}{c |}{res}         2 
{txt}      1994 {c |}{res}         0          0          2          6          0          2          0          2 {txt}{c |}{res}        12 
{txt}      1996 {c |}{res}         0          0          0          0          2          0          0          0 {txt}{c |}{res}         2 
{txt}      2004 {c |}{res}         0          0          0         10          0          2          2          0 {txt}{c |}{res}        14 
{txt}      2014 {c |}{res}         2          0          0          2          0          0          0          0 {txt}{c |}{res}         4 
{txt}{hline 11}{c +}{hline 88}{c +}{hline 10}
     Total {c |}{res}         8          2          4         36          2          4          2          2 {txt}{c |}{res}        60 
{txt}
{com}. *The only way this will be messed up is if there's re-redistricting in a year ending in 4, as there was in AK in 2014.  
. *states from above
. *except for half the cases in nd, all the below are from state senates.  
. *ak 76 86 14
. *co 84
. *hi 84 94
. *ia 72 84 94 04 14
. *mt 96
. *nd 94 04
. *or 04
. *ut 94
. drop if row==1&redistproblem==1
{txt}(30 observations deleted)

{com}. *COLLAPSE
. *collapse is necessary as some district-years are now observed twice.  
. gen c=1
{txt}
{com}. *VOTEMISS
. *Deal with Stata's problem with outputting "0" when summing when it should be sysmis.
. gen dvotemiss=dvote==.
{txt}
{com}. gen rvotemiss=rvote==.
{txt}
{com}. gen ovotemiss=ovote==.
{txt}
{com}. collapse (sum) c (mean) dper dvote rvote ovote cand inc inc2 inc3 other leg2 leg3 past switch switchwin stealth stealthwin propup partuncont mixeduncont uncont dwin minweight maxweight (min) lagyearmin=yearlag (max) dvotemiss rvotemiss ovotemiss lagyearmax=yearlag bigthird dontuse redistproblem [fweight=weight], by(year sid sen dname dno geopost mmdpost)
{txt}
{com}. *VOTEMISS
. *Deal with Stata's problem with outputting "0" when summing when it should be sysmis.
. replace dvote=. if dvotemiss==1
{txt}(0 real changes made)

{com}. replace rvote=. if rvotemiss==1
{txt}(0 real changes made)

{com}. replace ovote=. if ovotemiss==1
{txt}(0 real changes made)

{com}. drop dvotemiss rvotemiss ovotemiss
{txt}
{com}. *RENAME
. *Rename match vars
. rename dname dnamemerge
{res}{txt}
{com}. rename dno dnomerge
{res}{txt}
{com}. rename geopost geopostmerge
{res}{txt}
{com}. *rename vars with "lag" as the suffix
. foreach string in dper dvote rvote ovote cand inc inc2 inc3 other leg2 leg3 past switch switchwin stealth stealthwin propup bigthird dontuse partuncont mixeduncont uncont dwin minweight maxweight {c -(}
{txt}  2{com}. rename `string' `string'lag
{txt}  3{com}. {c )-}
{res}{txt}
{com}. drop c
{txt}
{com}. save tempslerslagged, replace
{txt}file tempslerslagged.dta saved

{com}. 
. *MAIN FILE
. *MERGE
. *Merge in lagged variables
. *First, alter vars that will be merged on when redist=2.  
. clear
{txt}
{com}. use $mainfile
{txt}
{com}. gen dnamemerge=dname
{txt}(126,204 missing values generated)

{com}. replace dnamemerge=redist1 if redist==2|redist==4|redist==7|redist==9
{txt}(27 real changes made)

{com}. gen dnomerge=dno
{txt}(1,997 missing values generated)

{com}. replace dnomerge=redist2 if redist==2|redist==4|redist==7|redist==9
{txt}(461 real changes made, 21 to missing)

{com}. gen geopostmerge=geopost
{txt}(135,641 missing values generated)

{com}. replace geopostmerge=redist3 if redist==2|redist==4|redist==7|redist==9
{txt}(2 real changes made)

{com}. *a many to 1 merge must be done because when redist=2, a district designation appears twice.  This won't hurt anything if everything is zeroed out that is redist=1.  The past value is being put into two districts, but since redist=1 for one, the inappropriately matched one will be changed to system missing.  
. merge m:1 year sid sen dnamemerge dnomerge geopostmerge mmdpost using tempslerslagged
{res}
{txt}{col 5}Result{col 38}# of obs.
{col 5}{hline 41}
{col 5}not matched{col 30}{res}          20,401
{txt}{col 9}from master{col 30}{res}          10,087{txt}  (_merge==1)
{col 9}from using{col 30}{res}          10,314{txt}  (_merge==2)

{col 5}matched{col 30}{res}         130,150{txt}  (_merge==3)
{col 5}{hline 41}

{com}. erase tempslerslagged.dta
{txt}
{com}. drop if year>2016
{txt}(6,675 observations deleted)

{com}. tab year _merge

           {txt}{c |}              _merge
      year {c |} master on  using onl  matched ( {c |}     Total
{hline 11}{c +}{hline 33}{c +}{hline 10}
      1968 {c |}{res}     4,463          0          0 {txt}{c |}{res}     4,463 
{txt}      1969 {c |}{res}       222          0          0 {txt}{c |}{res}       222 
{txt}      1970 {c |}{res}       951        159      3,821 {txt}{c |}{res}     4,931 
{txt}      1971 {c |}{res}       257         26        187 {txt}{c |}{res}       470 
{txt}      1972 {c |}{res}       984      1,099      3,654 {txt}{c |}{res}     5,737 
{txt}      1973 {c |}{res}        63         44        189 {txt}{c |}{res}       296 
{txt}      1974 {c |}{res}       755        364      4,303 {txt}{c |}{res}     5,422 
{txt}      1975 {c |}{res}        49         49        374 {txt}{c |}{res}       472 
{txt}      1976 {c |}{res}       124         38      4,875 {txt}{c |}{res}     5,037 
{txt}      1977 {c |}{res}         0          0        251 {txt}{c |}{res}       251 
{txt}      1978 {c |}{res}        50         94      5,008 {txt}{c |}{res}     5,152 
{txt}      1979 {c |}{res}       120        133        305 {txt}{c |}{res}       558 
{txt}      1980 {c |}{res}         0          0      4,924 {txt}{c |}{res}     4,924 
{txt}      1981 {c |}{res}         0          7        250 {txt}{c |}{res}       257 
{txt}      1982 {c |}{res}       536        372      4,869 {txt}{c |}{res}     5,777 
{txt}      1983 {c |}{res}         2          5        601 {txt}{c |}{res}       608 
{txt}      1984 {c |}{res}       204        138      4,975 {txt}{c |}{res}     5,317 
{txt}      1985 {c |}{res}         0          0        140 {txt}{c |}{res}       140 
{txt}      1986 {c |}{res}       146         23      5,424 {txt}{c |}{res}     5,593 
{txt}      1987 {c |}{res}         0          0        394 {txt}{c |}{res}       394 
{txt}      1988 {c |}{res}        14         16      5,321 {txt}{c |}{res}     5,351 
{txt}      1989 {c |}{res}         0          0        142 {txt}{c |}{res}       142 
{txt}      1990 {c |}{res}         2          6      5,539 {txt}{c |}{res}     5,547 
{txt}      1991 {c |}{res}         0          0        394 {txt}{c |}{res}       394 
{txt}      1992 {c |}{res}       383        279      5,421 {txt}{c |}{res}     6,083 
{txt}      1993 {c |}{res}         0          0        180 {txt}{c |}{res}       180 
{txt}      1994 {c |}{res}        67         57      5,547 {txt}{c |}{res}     5,671 
{txt}      1995 {c |}{res}         0          0        355 {txt}{c |}{res}       355 
{txt}      1996 {c |}{res}        12         11      5,496 {txt}{c |}{res}     5,519 
{txt}      1997 {c |}{res}         0          0        180 {txt}{c |}{res}       180 
{txt}      1998 {c |}{res}         1          2      5,613 {txt}{c |}{res}     5,616 
{txt}      1999 {c |}{res}         0          0        355 {txt}{c |}{res}       355 
{txt}      2000 {c |}{res}         2          1      5,467 {txt}{c |}{res}     5,470 
{txt}      2001 {c |}{res}         0          0        180 {txt}{c |}{res}       180 
{txt}      2002 {c |}{res}       279        398      5,373 {txt}{c |}{res}     6,050 
{txt}      2003 {c |}{res}         0          0        394 {txt}{c |}{res}       394 
{txt}      2004 {c |}{res}       127        110      5,184 {txt}{c |}{res}     5,421 
{txt}      2005 {c |}{res}         0          0        140 {txt}{c |}{res}       140 
{txt}      2006 {c |}{res}        16         17      5,536 {txt}{c |}{res}     5,569 
{txt}      2007 {c |}{res}         0          0        394 {txt}{c |}{res}       394 
{txt}      2008 {c |}{res}         0          0      5,323 {txt}{c |}{res}     5,323 
{txt}      2009 {c |}{res}         0          0        142 {txt}{c |}{res}       142 
{txt}      2010 {c |}{res}         3          1      5,544 {txt}{c |}{res}     5,548 
{txt}      2011 {c |}{res}         0          0        394 {txt}{c |}{res}       394 
{txt}      2012 {c |}{res}       195        129      5,415 {txt}{c |}{res}     5,739 
{txt}      2013 {c |}{res}         0          0        180 {txt}{c |}{res}       180 
{txt}      2014 {c |}{res}        44         47      5,537 {txt}{c |}{res}     5,628 
{txt}      2015 {c |}{res}         1          0        355 {txt}{c |}{res}       356 
{txt}      2016 {c |}{res}        14         15      5,505 {txt}{c |}{res}     5,534 
{txt}{hline 11}{c +}{hline 33}{c +}{hline 10}
     Total {c |}{res}    10,086      3,640    130,150 {txt}{c |}{res}   143,876 
{txt}
{com}. *For earlier years, the merge=2 cases cluster in redistricting years, as expected.
. *For merge=1, those are also clustered in redistricting years, as expected.  
. tab year _merge if sen==1|(sen==0&sid==34&year>1998)

           {txt}{c |}              _merge
      year {c |} master on  using onl  matched ( {c |}     Total
{hline 11}{c +}{hline 33}{c +}{hline 10}
      1968 {c |}{res}       934          0          0 {txt}{c |}{res}       934 
{txt}      1969 {c |}{res}        19          0          0 {txt}{c |}{res}        19 
{txt}      1970 {c |}{res}       614         37        413 {txt}{c |}{res}     1,064 
{txt}      1971 {c |}{res}       130          0          0 {txt}{c |}{res}       130 
{txt}      1972 {c |}{res}       185        199        933 {txt}{c |}{res}     1,317 
{txt}      1973 {c |}{res}        28          9         32 {txt}{c |}{res}        69 
{txt}      1974 {c |}{res}       150        107        872 {txt}{c |}{res}     1,129 
{txt}      1975 {c |}{res}         6          6        103 {txt}{c |}{res}       115 
{txt}      1976 {c |}{res}        81          4        999 {txt}{c |}{res}     1,084 
{txt}      1977 {c |}{res}         0          0         59 {txt}{c |}{res}        59 
{txt}      1978 {c |}{res}        11         11      1,011 {txt}{c |}{res}     1,033 
{txt}      1979 {c |}{res}        26         32         85 {txt}{c |}{res}       143 
{txt}      1980 {c |}{res}         0          0      1,053 {txt}{c |}{res}     1,053 
{txt}      1981 {c |}{res}         0          0         61 {txt}{c |}{res}        61 
{txt}      1982 {c |}{res}        86         70      1,064 {txt}{c |}{res}     1,220 
{txt}      1983 {c |}{res}         2          0        234 {txt}{c |}{res}       236 
{txt}      1984 {c |}{res}       115         88        918 {txt}{c |}{res}     1,121 
{txt}      1986 {c |}{res}        34         21      1,114 {txt}{c |}{res}     1,169 
{txt}      1987 {c |}{res}         0          0        132 {txt}{c |}{res}       132 
{txt}      1988 {c |}{res}         9          9      1,076 {txt}{c |}{res}     1,094 
{txt}      1989 {c |}{res}         0          0          2 {txt}{c |}{res}         2 
{txt}      1990 {c |}{res}         0          0      1,125 {txt}{c |}{res}     1,125 
{txt}      1991 {c |}{res}         0          0        132 {txt}{c |}{res}       132 
{txt}      1992 {c |}{res}        74         71      1,246 {txt}{c |}{res}     1,391 
{txt}      1993 {c |}{res}         0          0         40 {txt}{c |}{res}        40 
{txt}      1994 {c |}{res}        47         42      1,033 {txt}{c |}{res}     1,122 
{txt}      1995 {c |}{res}         0          0         93 {txt}{c |}{res}        93 
{txt}      1996 {c |}{res}        12         11      1,134 {txt}{c |}{res}     1,157 
{txt}      1997 {c |}{res}         0          0         40 {txt}{c |}{res}        40 
{txt}      1998 {c |}{res}         0          0      1,081 {txt}{c |}{res}     1,081 
{txt}      1999 {c |}{res}         0          0         93 {txt}{c |}{res}        93 
{txt}      2000 {c |}{res}         0          0      1,156 {txt}{c |}{res}     1,156 
{txt}      2001 {c |}{res}         0          0         40 {txt}{c |}{res}        40 
{txt}      2002 {c |}{res}        43         52      1,233 {txt}{c |}{res}     1,328 
{txt}      2003 {c |}{res}         0          0        132 {txt}{c |}{res}       132 
{txt}      2004 {c |}{res}        37         31      1,055 {txt}{c |}{res}     1,123 
{txt}      2006 {c |}{res}        13         13      1,149 {txt}{c |}{res}     1,175 
{txt}      2007 {c |}{res}         0          0        132 {txt}{c |}{res}       132 
{txt}      2008 {c |}{res}         0          0      1,103 {txt}{c |}{res}     1,103 
{txt}      2009 {c |}{res}         0          0          2 {txt}{c |}{res}         2 
{txt}      2010 {c |}{res}         0          0      1,155 {txt}{c |}{res}     1,155 
{txt}      2011 {c |}{res}         0          0        132 {txt}{c |}{res}       132 
{txt}      2012 {c |}{res}        24         29      1,260 {txt}{c |}{res}     1,313 
{txt}      2013 {c |}{res}         0          0         40 {txt}{c |}{res}        40 
{txt}      2014 {c |}{res}        27         25      1,057 {txt}{c |}{res}     1,109 
{txt}      2015 {c |}{res}         0          0         93 {txt}{c |}{res}        93 
{txt}      2016 {c |}{res}        14         14      1,180 {txt}{c |}{res}     1,208 
{txt}{hline 11}{c +}{hline 33}{c +}{hline 10}
     Total {c |}{res}     2,721        881     27,097 {txt}{c |}{res}    30,699 
{txt}
{com}. tab year _merge if sen==0&(!(sid==34&year>1998))

           {txt}{c |}              _merge
      year {c |} master on  using onl  matched ( {c |}     Total
{hline 11}{c +}{hline 33}{c +}{hline 10}
      1968 {c |}{res}     3,529          0          0 {txt}{c |}{res}     3,529 
{txt}      1969 {c |}{res}       203          0          0 {txt}{c |}{res}       203 
{txt}      1970 {c |}{res}       337        122      3,408 {txt}{c |}{res}     3,867 
{txt}      1971 {c |}{res}       127         26        187 {txt}{c |}{res}       340 
{txt}      1972 {c |}{res}       799        900      2,721 {txt}{c |}{res}     4,420 
{txt}      1973 {c |}{res}        35         35        157 {txt}{c |}{res}       227 
{txt}      1974 {c |}{res}       605        257      3,431 {txt}{c |}{res}     4,293 
{txt}      1975 {c |}{res}        43         43        271 {txt}{c |}{res}       357 
{txt}      1976 {c |}{res}        43         34      3,876 {txt}{c |}{res}     3,953 
{txt}      1977 {c |}{res}         0          0        192 {txt}{c |}{res}       192 
{txt}      1978 {c |}{res}        39         83      3,997 {txt}{c |}{res}     4,119 
{txt}      1979 {c |}{res}        94        101        220 {txt}{c |}{res}       415 
{txt}      1980 {c |}{res}         0          0      3,871 {txt}{c |}{res}     3,871 
{txt}      1981 {c |}{res}         0          7        189 {txt}{c |}{res}       196 
{txt}      1982 {c |}{res}       450        302      3,805 {txt}{c |}{res}     4,557 
{txt}      1983 {c |}{res}         0          5        367 {txt}{c |}{res}       372 
{txt}      1984 {c |}{res}        89         50      4,057 {txt}{c |}{res}     4,196 
{txt}      1985 {c |}{res}         0          0        140 {txt}{c |}{res}       140 
{txt}      1986 {c |}{res}       112          2      4,310 {txt}{c |}{res}     4,424 
{txt}      1987 {c |}{res}         0          0        262 {txt}{c |}{res}       262 
{txt}      1988 {c |}{res}         5          7      4,245 {txt}{c |}{res}     4,257 
{txt}      1989 {c |}{res}         0          0        140 {txt}{c |}{res}       140 
{txt}      1990 {c |}{res}         2          6      4,414 {txt}{c |}{res}     4,422 
{txt}      1991 {c |}{res}         0          0        262 {txt}{c |}{res}       262 
{txt}      1992 {c |}{res}       309        208      4,175 {txt}{c |}{res}     4,692 
{txt}      1993 {c |}{res}         0          0        140 {txt}{c |}{res}       140 
{txt}      1994 {c |}{res}        20         15      4,514 {txt}{c |}{res}     4,549 
{txt}      1995 {c |}{res}         0          0        262 {txt}{c |}{res}       262 
{txt}      1996 {c |}{res}         0          0      4,362 {txt}{c |}{res}     4,362 
{txt}      1997 {c |}{res}         0          0        140 {txt}{c |}{res}       140 
{txt}      1998 {c |}{res}         1          2      4,532 {txt}{c |}{res}     4,535 
{txt}      1999 {c |}{res}         0          0        262 {txt}{c |}{res}       262 
{txt}      2000 {c |}{res}         2          1      4,311 {txt}{c |}{res}     4,314 
{txt}      2001 {c |}{res}         0          0        140 {txt}{c |}{res}       140 
{txt}      2002 {c |}{res}       236        346      4,140 {txt}{c |}{res}     4,722 
{txt}      2003 {c |}{res}         0          0        262 {txt}{c |}{res}       262 
{txt}      2004 {c |}{res}        90         79      4,129 {txt}{c |}{res}     4,298 
{txt}      2005 {c |}{res}         0          0        140 {txt}{c |}{res}       140 
{txt}      2006 {c |}{res}         3          4      4,387 {txt}{c |}{res}     4,394 
{txt}      2007 {c |}{res}         0          0        262 {txt}{c |}{res}       262 
{txt}      2008 {c |}{res}         0          0      4,220 {txt}{c |}{res}     4,220 
{txt}      2009 {c |}{res}         0          0        140 {txt}{c |}{res}       140 
{txt}      2010 {c |}{res}         3          1      4,389 {txt}{c |}{res}     4,393 
{txt}      2011 {c |}{res}         0          0        262 {txt}{c |}{res}       262 
{txt}      2012 {c |}{res}       171        100      4,155 {txt}{c |}{res}     4,426 
{txt}      2013 {c |}{res}         0          0        140 {txt}{c |}{res}       140 
{txt}      2014 {c |}{res}        17         22      4,480 {txt}{c |}{res}     4,519 
{txt}      2015 {c |}{res}         1          0        262 {txt}{c |}{res}       263 
{txt}      2016 {c |}{res}         0          1      4,325 {txt}{c |}{res}     4,326 
{txt}{hline 11}{c +}{hline 33}{c +}{hline 10}
     Total {c |}{res}     7,365      2,759    103,053 {txt}{c |}{res}   113,177 
{txt}
{com}. tab sid _merge if sen==0&_merge!=3

 {txt}number of {c |}
  state in {c |}        _merge
alpha list {c |} master on  using onl {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
         1 {c |}{res}       193         92 {txt}{c |}{res}       285 
{txt}         2 {c |}{res}        78         43 {txt}{c |}{res}       121 
{txt}         3 {c |}{res}        30          0 {txt}{c |}{res}        30 
{txt}         4 {c |}{res}       266        168 {txt}{c |}{res}       434 
{txt}         5 {c |}{res}        80          1 {txt}{c |}{res}        81 
{txt}         6 {c |}{res}        65          0 {txt}{c |}{res}        65 
{txt}         7 {c |}{res}       177         26 {txt}{c |}{res}       203 
{txt}         8 {c |}{res}        41          0 {txt}{c |}{res}        41 
{txt}         9 {c |}{res}       313        193 {txt}{c |}{res}       506 
{txt}        10 {c |}{res}       491        332 {txt}{c |}{res}       823 
{txt}        11 {c |}{res}        49          0 {txt}{c |}{res}        49 
{txt}        12 {c |}{res}        71         38 {txt}{c |}{res}       109 
{txt}        13 {c |}{res}       118          0 {txt}{c |}{res}       118 
{txt}        14 {c |}{res}       139         39 {txt}{c |}{res}       178 
{txt}        15 {c |}{res}       222        122 {txt}{c |}{res}       344 
{txt}        16 {c |}{res}       125          2 {txt}{c |}{res}       127 
{txt}        17 {c |}{res}       100          4 {txt}{c |}{res}       104 
{txt}        19 {c |}{res}       183         32 {txt}{c |}{res}       215 
{txt}        20 {c |}{res}       179        112 {txt}{c |}{res}       291 
{txt}        21 {c |}{res}       248         88 {txt}{c |}{res}       336 
{txt}        22 {c |}{res}       110          4 {txt}{c |}{res}       114 
{txt}        23 {c |}{res}       134          3 {txt}{c |}{res}       137 
{txt}        24 {c |}{res}       259        145 {txt}{c |}{res}       404 
{txt}        25 {c |}{res}       163          0 {txt}{c |}{res}       163 
{txt}        26 {c |}{res}       118         23 {txt}{c |}{res}       141 
{txt}        28 {c |}{res}       119         77 {txt}{c |}{res}       196 
{txt}        29 {c |}{res}       447        263 {txt}{c |}{res}       710 
{txt}        30 {c |}{res}        78         38 {txt}{c |}{res}       116 
{txt}        31 {c |}{res}       139         69 {txt}{c |}{res}       208 
{txt}        32 {c |}{res}       150          3 {txt}{c |}{res}       153 
{txt}        33 {c |}{res}       177         77 {txt}{c |}{res}       254 
{txt}        34 {c |}{res}        55          8 {txt}{c |}{res}        63 
{txt}        35 {c |}{res}        99         10 {txt}{c |}{res}       109 
{txt}        36 {c |}{res}       101          0 {txt}{c |}{res}       101 
{txt}        37 {c |}{res}       100         40 {txt}{c |}{res}       140 
{txt}        38 {c |}{res}       203          0 {txt}{c |}{res}       203 
{txt}        39 {c |}{res}       100         25 {txt}{c |}{res}       125 
{txt}        40 {c |}{res}       168         44 {txt}{c |}{res}       212 
{txt}        41 {c |}{res}        49         20 {txt}{c |}{res}        69 
{txt}        42 {c |}{res}       176         77 {txt}{c |}{res}       253 
{txt}        43 {c |}{res}       334        188 {txt}{c |}{res}       522 
{txt}        44 {c |}{res}        75          1 {txt}{c |}{res}        76 
{txt}        45 {c |}{res}       231        134 {txt}{c |}{res}       365 
{txt}        46 {c |}{res}       116         27 {txt}{c |}{res}       143 
{txt}        47 {c |}{res}       121         23 {txt}{c |}{res}       144 
{txt}        48 {c |}{res}        95         38 {txt}{c |}{res}       133 
{txt}        49 {c |}{res}       198        110 {txt}{c |}{res}       308 
{txt}        50 {c |}{res}        83         23 {txt}{c |}{res}       106 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}     7,366      2,762 {txt}{c |}{res}    10,128 
{txt}
{com}. tab year redist if _merge==1&sen==0&(!(sid==34&year>1998))

           {txt}{c |}      redistricing variable
      year {c |}         0          1          2 {c |}     Total
{hline 11}{c +}{hline 33}{c +}{hline 10}
      1968 {c |}{res}     2,845        684          0 {txt}{c |}{res}     3,529 
{txt}      1969 {c |}{res}       162         41          0 {txt}{c |}{res}       203 
{txt}      1970 {c |}{res}       230        107          0 {txt}{c |}{res}       337 
{txt}      1971 {c |}{res}         0        127          0 {txt}{c |}{res}       127 
{txt}      1972 {c |}{res}         0        792          7 {txt}{c |}{res}       799 
{txt}      1973 {c |}{res}         0         35          0 {txt}{c |}{res}        35 
{txt}      1974 {c |}{res}       134        471          0 {txt}{c |}{res}       605 
{txt}      1975 {c |}{res}         0         43          0 {txt}{c |}{res}        43 
{txt}      1976 {c |}{res}         0         43          0 {txt}{c |}{res}        43 
{txt}      1978 {c |}{res}         6         33          0 {txt}{c |}{res}        39 
{txt}      1979 {c |}{res}         0         94          0 {txt}{c |}{res}        94 
{txt}      1982 {c |}{res}         0        450          0 {txt}{c |}{res}       450 
{txt}      1984 {c |}{res}         0         89          0 {txt}{c |}{res}        89 
{txt}      1986 {c |}{res}       112          0          0 {txt}{c |}{res}       112 
{txt}      1988 {c |}{res}         0          5          0 {txt}{c |}{res}         5 
{txt}      1990 {c |}{res}         2          0          0 {txt}{c |}{res}         2 
{txt}      1992 {c |}{res}         0        308          1 {txt}{c |}{res}       309 
{txt}      1994 {c |}{res}         0         20          0 {txt}{c |}{res}        20 
{txt}      1998 {c |}{res}         0          1          0 {txt}{c |}{res}         1 
{txt}      2000 {c |}{res}         0          2          0 {txt}{c |}{res}         2 
{txt}      2002 {c |}{res}         0        236          0 {txt}{c |}{res}       236 
{txt}      2004 {c |}{res}         0         90          0 {txt}{c |}{res}        90 
{txt}      2006 {c |}{res}         1          2          0 {txt}{c |}{res}         3 
{txt}      2010 {c |}{res}         3          0          0 {txt}{c |}{res}         3 
{txt}      2012 {c |}{res}         0        171          0 {txt}{c |}{res}       171 
{txt}      2014 {c |}{res}         0         17          0 {txt}{c |}{res}        17 
{txt}      2015 {c |}{res}         1          0          0 {txt}{c |}{res}         1 
{txt}{hline 11}{c +}{hline 33}{c +}{hline 10}
     Total {c |}{res}     3,496      3,861          8 {txt}{c |}{res}     7,365 
{txt}
{com}. *I believe the merge=1 cases are orphans, the counter-parts of redist=2, 4, 7 and 9 cases.  Say district #4 became district #2.  Then after redistricting, contemporaneous district #4 would receive no lagged value (which doesn't matter, since it would be changed to system missing anyway).  This is consistent with the fact that only eight contests with redist=2 are merge=1 for state houses, out of 7470 merge=1 cases.  
. *Merge=2 cases can't be dropped yet, to do the below.  
. drop if _merge==2
{txt}(3,640 observations deleted)

{com}. drop dnamemerge dnomerge geopostmerge _merge
{txt}
{com}. 
. *YEARLAG
. gen tempdif=lagyearmax!=lagyearmin
{txt}
{com}. tab tempdif redistproblem

           {txt}{c |}  (max) redistproblem
   tempdif {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
         0 {c |}{res}   130,115         29 {txt}{c |}{res}   130,144 
{txt}         1 {c |}{res}         6          0 {txt}{c |}{res}         6 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}   130,121         29 {txt}{c |}{res}   130,150 
{txt}
{com}. *redistproblem is always associated with tempdif=0, good.
. assert tempdif==0 if redistproblem==1
{txt}
{com}. tab redist if redistproblem==1

{txt}redistricin {c |}
 g variable {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}         21       72.41       72.41
{txt}          1 {c |}{res}          6       20.69       93.10
{txt}          4 {c |}{res}          1        3.45       96.55
{txt}          8 {c |}{res}          1        3.45      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}         29      100.00
{txt}
{com}. *redist=0 for 21 cases, redist=1 for 6 cases, redist=4 for 1 case, and redist=8 for 1 case.  Redist=0 is no problem.  The redist=1 cases are no problem, they will simply be changed to system missing like all the other lagged values in that circumstance.  redist=4 is a combo of redist=2 and redist=3, and I don't see any problem with that.  redist=8 isn't a problem, the earlier value of the pair has been dropped.  
. *There are 6 cases of tempdif=2.
. sort sid sen dname dno geopost mmdpost year
{txt}
{com}. list sid sen dname dno geopost mmdpost year tempdif if tempdif>0&tempdif!=.
{txt}
        {c TLC}{hline 5}{c -}{hline 5}{c -}{hline 17}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 6}{c -}{hline 9}{c TRC}
        {c |} {res}sid   sen             dname   dno   geopost   mmdpost   year   tempdif {txt}{c |}
        {c LT}{hline 5}{c -}{hline 5}{c -}{hline 17}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 6}{c -}{hline 9}{c RT}
 28913. {c |} {res} 11     1                       7         .         .   1978         1 {txt}{c |}
 28915. {c |} {res} 11     1                       7         .         .   1982         1 {txt}{c |}
 93434. {c |} {res} 34     0                       3         .         .   2006         1 {txt}{c |}
 93841. {c |} {res} 34     0                      23         .         .   2006         1 {txt}{c |}
 93883. {c |} {res} 34     0                      25         .         .   2006         1 {txt}{c |}
        {c LT}{hline 5}{c -}{hline 5}{c -}{hline 17}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 6}{c -}{hline 9}{c RT}
140113. {c |} {res} 50     1   campbelljohnson     .         .         .   1980         1 {txt}{c |}
        {c BLC}{hline 5}{c -}{hline 5}{c -}{hline 17}{c -}{hline 5}{c -}{hline 9}{c -}{hline 9}{c -}{hline 6}{c -}{hline 9}{c BRC}

{com}. *None of those are problematic, they are different streams coming together as they should.  
. drop tempdif redistproblem
{txt}
{com}. 
. *LAGS, REDIST & MISS
. *Make vars sysmis if redistricting occurred.  Note that the year the lagged value is coming from has to be turned to system missing if there was redistricting.  Many of these will be filled in after the below.  
. foreach string in dperlag candlag inclag inc2lag inc3lag otherlag leg2lag leg3lag pastlag switchlag switchwinlag stealthlag stealthwinlag partuncontlag mixeduncontlag uncontlag dwinlag propuplag bigthirdlag dontuselag lagyearmin lagyearmax {c -(}
{txt}  2{com}. replace `string'=. if redist==1|redist==6|redist==8
{txt}  3{com}. {c )-}
{txt}(30,817 real changes made, 30,817 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)
(30,875 real changes made, 30,875 to missing)

{com}. 
. *Fill in lagyearmin and lagyearmax when possible.  
. *If termz is always "4" in a chamber, then make lagyearmin/max always equal year-4.  Same for always "2."  
. egen mintermz2=min(mintermz), by(sid sen)
{txt}
{com}. egen maxtermz2=max(maxtermz), by(sid sen)
{txt}
{com}. forvalues aaa=2(2)4 {c -(}
{txt}  2{com}. gen always`aaa'=(mintermz2==maxtermz2)&mintermz==`aaa'
{txt}  3{com}. foreach string in min max {c -(}
{txt}  4{com}. replace lagyear`string'=year-`aaa' if always`aaa'==1&lagyear`string'==.
{txt}  5{com}. {c )-}
{txt}  6{com}. drop always`aaa'
{txt}  7{com}. {c )-}
{txt}(27,956 real changes made)
(27,956 real changes made)
(902 real changes made)
(902 real changes made)

{com}. drop mintermz2 maxtermz2
{txt}
{com}. tab sid if lagyearmin==.&sen==0

  {txt}number of {c |}
   state in {c |}
 alpha list {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          1 {c |}{res}        726       18.60       18.60
{txt}          9 {c |}{res}        723       18.52       37.13
{txt}         17 {c |}{res}        731       18.73       55.85
{txt}         24 {c |}{res}        845       21.65       77.50
{txt}         34 {c |}{res}        307        7.87       85.37
{txt}         46 {c |}{res}        571       14.63      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      3,903      100.00
{txt}
{com}. *some are left, I can see why.  etype=ssg elections.  
. gen miss=lagyearmin==.&firstcase==0
{txt}
{com}. foreach num in 1 9 17 18 24 46 {c -(}
{txt}  2{com}. di "`num'"
{txt}  3{com}. tab year miss if sen==0&sid==`num'
{txt}  4{com}. {c )-}
1

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1970 {c |}{res}       106          0 {txt}{c |}{res}       106 
{txt}      1974 {c |}{res}         0        105 {txt}{c |}{res}       105 
{txt}      1978 {c |}{res}       105          0 {txt}{c |}{res}       105 
{txt}      1982 {c |}{res}         0        105 {txt}{c |}{res}       105 
{txt}      1983 {c |}{res}         5        100 {txt}{c |}{res}       105 
{txt}      1986 {c |}{res}       105          0 {txt}{c |}{res}       105 
{txt}      1990 {c |}{res}       105          0 {txt}{c |}{res}       105 
{txt}      1994 {c |}{res}         4        101 {txt}{c |}{res}       105 
{txt}      1998 {c |}{res}       105          0 {txt}{c |}{res}       105 
{txt}      2002 {c |}{res}         1        104 {txt}{c |}{res}       105 
{txt}      2006 {c |}{res}       105          0 {txt}{c |}{res}       105 
{txt}      2010 {c |}{res}       105          0 {txt}{c |}{res}       105 
{txt}      2014 {c |}{res}         0        105 {txt}{c |}{res}       105 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       746        620 {txt}{c |}{res}     1,366 
9

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}       119          0 {txt}{c |}{res}       119 
{txt}      1970 {c |}{res}       119          0 {txt}{c |}{res}       119 
{txt}      1972 {c |}{res}         0        120 {txt}{c |}{res}       120 
{txt}      1974 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      1976 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      1978 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      1980 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      1982 {c |}{res}         0        120 {txt}{c |}{res}       120 
{txt}      1984 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      1986 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      1988 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      1990 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      1992 {c |}{res}         0        120 {txt}{c |}{res}       120 
{txt}      1994 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      1996 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      1998 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      2000 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      2002 {c |}{res}         0        120 {txt}{c |}{res}       120 
{txt}      2004 {c |}{res}       117          3 {txt}{c |}{res}       120 
{txt}      2006 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      2008 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      2010 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}      2012 {c |}{res}         0        120 {txt}{c |}{res}       120 
{txt}      2014 {c |}{res}       119          0 {txt}{c |}{res}       119 
{txt}      2015 {c |}{res}         0          1 {txt}{c |}{res}         1 
{txt}      2016 {c |}{res}       120          0 {txt}{c |}{res}       120 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}     2,394        604 {txt}{c |}{res}     2,998 
17

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1969 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1971 {c |}{res}        19         81 {txt}{c |}{res}       100 
{txt}      1973 {c |}{res}        11         89 {txt}{c |}{res}       100 
{txt}      1975 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1977 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1979 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1981 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1984 {c |}{res}         2         98 {txt}{c |}{res}       100 
{txt}      1986 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1988 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1990 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1992 {c |}{res}         1         99 {txt}{c |}{res}       100 
{txt}      1994 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1996 {c |}{res}        31         69 {txt}{c |}{res}       100 
{txt}      1998 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      2000 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      2002 {c |}{res}         5         95 {txt}{c |}{res}       100 
{txt}      2004 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      2006 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      2008 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      2010 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      2012 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      2014 {c |}{res}         0        100 {txt}{c |}{res}       100 
{txt}      2016 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}     1,769        631 {txt}{c |}{res}     2,400 
18
{txt}no observations
24

           {c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1971 {c |}{res}       122          0 {txt}{c |}{res}       122 
{txt}      1975 {c |}{res}         0        122 {txt}{c |}{res}       122 
{txt}      1979 {c |}{res}         7        115 {txt}{c |}{res}       122 
{txt}      1983 {c |}{res}         2        120 {txt}{c |}{res}       122 
{txt}      1987 {c |}{res}       122          0 {txt}{c |}{res}       122 
{txt}      1991 {c |}{res}       122          0 {txt}{c |}{res}       122 
{txt}      1992 {c |}{res}         0        122 {txt}{c |}{res}       122 
{txt}      1995 {c |}{res}       122          0 {txt}{c |}{res}       122 
{txt}      1999 {c |}{res}       122          0 {txt}{c |}{res}       122 
{txt}      2003 {c |}{res}         0        122 {txt}{c |}{res}       122 
{txt}      2007 {c |}{res}       122          0 {txt}{c |}{res}       122 
{txt}      2011 {c |}{res}       122          0 {txt}{c |}{res}       122 
{txt}      2015 {c |}{res}         0        122 {txt}{c |}{res}       122 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       863        723 {txt}{c |}{res}     1,586 
46

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1969 {c |}{res}        62          0 {txt}{c |}{res}        62 
{txt}      1971 {c |}{res}         7         42 {txt}{c |}{res}        49 
{txt}      1973 {c |}{res}        25         27 {txt}{c |}{res}        52 
{txt}      1975 {c |}{res}        52          0 {txt}{c |}{res}        52 
{txt}      1977 {c |}{res}        52          0 {txt}{c |}{res}        52 
{txt}      1979 {c |}{res}        52          0 {txt}{c |}{res}        52 
{txt}      1981 {c |}{res}         7         42 {txt}{c |}{res}        49 
{txt}      1982 {c |}{res}         1         99 {txt}{c |}{res}       100 
{txt}      1983 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1985 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1987 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1989 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1991 {c |}{res}         0        100 {txt}{c |}{res}       100 
{txt}      1993 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1995 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1997 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      1999 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      2001 {c |}{res}         0        100 {txt}{c |}{res}       100 
{txt}      2003 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      2005 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      2007 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      2009 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      2011 {c |}{res}         1         99 {txt}{c |}{res}       100 
{txt}      2013 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}      2015 {c |}{res}       100          0 {txt}{c |}{res}       100 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}     1,659        509 {txt}{c |}{res}     2,168 
{txt}
{com}. *The above implies the following will work.  
. replace lagyearmin=1982 if sen==0&sid==1&year==1983
{txt}(100 real changes made)

{com}. replace lagyearmin=1981 if sen==0&sid==17&year==1984
{txt}(98 real changes made)

{com}. replace lagyearmin=1991 if sen==0&sid==24&year==1992
{txt}(122 real changes made)

{com}. replace lagyearmin=1981 if sen==0&sid==46&year==1982
{txt}(99 real changes made)

{com}. replace lagyearmin=year-4 if sen==0&(sid==1|sid==18|sid==24)&lagyearmin==.
{txt}(1,349 real changes made)

{com}. replace lagyearmin=year-2 if sen==0&(sid==9|sid==17|sid==46)&lagyearmin==.
{txt}(1,828 real changes made)

{com}. replace lagyearmax=lagyearmin if lagyearmax==.
{txt}(3,596 real changes made)

{com}. tab sid if lagyearmin==.&sen==0

  {txt}number of {c |}
   state in {c |}
 alpha list {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
         34 {c |}{res}        307      100.00      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}        307      100.00
{txt}
{com}. *only nd remains, and I can't do much with that.  
. *SEN
. tab sid if lagyearmin==.&sen==1

  {txt}number of {c |}
   state in {c |}
 alpha list {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          1 {c |}{res}        244        2.98        2.98
{txt}          2 {c |}{res}        140        1.71        4.68
{txt}          4 {c |}{res}        212        2.59        7.27
{txt}          6 {c |}{res}        207        2.52        9.79
{txt}          8 {c |}{res}        124        1.51       11.30
{txt}          9 {c |}{res}        268        3.27       14.57
{txt}         11 {c |}{res}        130        1.59       16.16
{txt}         13 {c |}{res}        353        4.30       20.46
{txt}         14 {c |}{res}        288        3.51       23.98
{txt}         15 {c |}{res}        309        3.77       27.74
{txt}         16 {c |}{res}        232        2.83       30.57
{txt}         17 {c |}{res}        248        3.02       33.60
{txt}         22 {c |}{res}        225        2.74       36.34
{txt}         23 {c |}{res}        335        4.09       40.43
{txt}         24 {c |}{res}        355        4.33       44.76
{txt}         25 {c |}{res}        200        2.44       47.20
{txt}         26 {c |}{res}        307        3.74       50.94
{txt}         28 {c |}{res}        112        1.37       52.30
{txt}         30 {c |}{res}        218        2.66       54.96
{txt}         31 {c |}{res}        225        2.74       57.71
{txt}         34 {c |}{res}        308        3.76       61.46
{txt}         35 {c |}{res}        194        2.37       63.83
{txt}         36 {c |}{res}        287        3.50       67.33
{txt}         37 {c |}{res}        177        2.16       69.49
{txt}         38 {c |}{res}        296        3.61       73.10
{txt}         39 {c |}{res}        288        3.51       76.61
{txt}         40 {c |}{res}        264        3.22       79.83
{txt}         42 {c |}{res}        230        2.80       82.63
{txt}         43 {c |}{res}        234        2.85       85.49
{txt}         44 {c |}{res}        173        2.11       87.60
{txt}         46 {c |}{res}        197        2.40       90.00
{txt}         47 {c |}{res}        300        3.66       93.66
{txt}         48 {c |}{res}        182        2.22       95.88
{txt}         49 {c |}{res}        210        2.56       98.44
{txt}         50 {c |}{res}        128        1.56      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      8,200      100.00
{txt}
{com}. *Of the states in that list, the following are the only ones I can do something about.  
. foreach num in 1 16 18 22 23 24 30 31 39 40 46 {c -(}
{txt}  2{com}. di "`num'"
{txt}  3{com}. tab year miss if sen==1&sid==`num'
{txt}  4{com}. {c )-}
1

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1970 {c |}{res}        35          0 {txt}{c |}{res}        35 
{txt}      1974 {c |}{res}         0         35 {txt}{c |}{res}        35 
{txt}      1978 {c |}{res}        35          0 {txt}{c |}{res}        35 
{txt}      1982 {c |}{res}         0         35 {txt}{c |}{res}        35 
{txt}      1983 {c |}{res}         0         35 {txt}{c |}{res}        35 
{txt}      1986 {c |}{res}        35          0 {txt}{c |}{res}        35 
{txt}      1990 {c |}{res}        35          0 {txt}{c |}{res}        35 
{txt}      1994 {c |}{res}         1         34 {txt}{c |}{res}        35 
{txt}      1998 {c |}{res}        35          0 {txt}{c |}{res}        35 
{txt}      2002 {c |}{res}         0         35 {txt}{c |}{res}        35 
{txt}      2006 {c |}{res}        35          0 {txt}{c |}{res}        35 
{txt}      2010 {c |}{res}        35          0 {txt}{c |}{res}        35 
{txt}      2014 {c |}{res}         0         35 {txt}{c |}{res}        35 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       246        209 {txt}{c |}{res}       455 
16

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1972 {c |}{res}         5         35 {txt}{c |}{res}        40 
{txt}      1974 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1976 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1978 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1980 {c |}{res}         2         38 {txt}{c |}{res}        40 
{txt}      1984 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1988 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1992 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      1996 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1998 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      2000 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      2004 {c |}{res}         1         39 {txt}{c |}{res}        40 
{txt}      2008 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      2010 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      2012 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      2014 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      2016 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       336        192 {txt}{c |}{res}       528 
18
{txt}no observations
22

           {c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1970 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}      1974 {c |}{res}         0         38 {txt}{c |}{res}        38 
{txt}      1978 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}      1982 {c |}{res}         0         38 {txt}{c |}{res}        38 
{txt}      1986 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}      1990 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}      1994 {c |}{res}         2         36 {txt}{c |}{res}        38 
{txt}      1998 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}      2002 {c |}{res}         1         37 {txt}{c |}{res}        38 
{txt}      2006 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}      2010 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}      2014 {c |}{res}         0         38 {txt}{c |}{res}        38 
{txt}      2016 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       271        187 {txt}{c |}{res}       458 
23

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1974 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      1976 {c |}{res}        67          0 {txt}{c |}{res}        67 
{txt}      1980 {c |}{res}        67          0 {txt}{c |}{res}        67 
{txt}      1982 {c |}{res}         0         67 {txt}{c |}{res}        67 
{txt}      1986 {c |}{res}        67          0 {txt}{c |}{res}        67 
{txt}      1990 {c |}{res}        67          0 {txt}{c |}{res}        67 
{txt}      1992 {c |}{res}         0         67 {txt}{c |}{res}        67 
{txt}      1996 {c |}{res}        67          0 {txt}{c |}{res}        67 
{txt}      2000 {c |}{res}        67          0 {txt}{c |}{res}        67 
{txt}      2002 {c |}{res}         0         67 {txt}{c |}{res}        67 
{txt}      2006 {c |}{res}        67          0 {txt}{c |}{res}        67 
{txt}      2008 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      2010 {c |}{res}        67          0 {txt}{c |}{res}        67 
{txt}      2012 {c |}{res}         0         67 {txt}{c |}{res}        67 
{txt}      2016 {c |}{res}        67          0 {txt}{c |}{res}        67 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       607        268 {txt}{c |}{res}       875 
24

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1971 {c |}{res}        52          0 {txt}{c |}{res}        52 
{txt}      1975 {c |}{res}         0         52 {txt}{c |}{res}        52 
{txt}      1979 {c |}{res}         8         44 {txt}{c |}{res}        52 
{txt}      1983 {c |}{res}         0         52 {txt}{c |}{res}        52 
{txt}      1987 {c |}{res}        52          0 {txt}{c |}{res}        52 
{txt}      1991 {c |}{res}        52          0 {txt}{c |}{res}        52 
{txt}      1992 {c |}{res}         1         51 {txt}{c |}{res}        52 
{txt}      1995 {c |}{res}        52          0 {txt}{c |}{res}        52 
{txt}      1999 {c |}{res}        52          0 {txt}{c |}{res}        52 
{txt}      2003 {c |}{res}         0         52 {txt}{c |}{res}        52 
{txt}      2007 {c |}{res}        52          0 {txt}{c |}{res}        52 
{txt}      2011 {c |}{res}        52          0 {txt}{c |}{res}        52 
{txt}      2015 {c |}{res}         0         52 {txt}{c |}{res}        52 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       373        303 {txt}{c |}{res}       676 
30

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1971 {c |}{res}        20          1 {txt}{c |}{res}        21 
{txt}      1973 {c |}{res}         1         39 {txt}{c |}{res}        40 
{txt}      1977 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1979 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      1981 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      1983 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1987 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1989 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      1991 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      1993 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1995 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1997 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1999 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      2001 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      2003 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      2007 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      2009 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      2011 {c |}{res}         2         38 {txt}{c |}{res}        40 
{txt}      2013 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      2015 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       352        198 {txt}{c |}{res}       550 
31

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1970 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      1972 {c |}{res}        28          6 {txt}{c |}{res}        34 
{txt}      1974 {c |}{res}         8          0 {txt}{c |}{res}         8 
{txt}      1976 {c |}{res}        35          0 {txt}{c |}{res}        35 
{txt}      1978 {c |}{res}         9          0 {txt}{c |}{res}         9 
{txt}      1980 {c |}{res}        36          0 {txt}{c |}{res}        36 
{txt}      1984 {c |}{res}         1         41 {txt}{c |}{res}        42 
{txt}      1986 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      1988 {c |}{res}        42          0 {txt}{c |}{res}        42 
{txt}      1990 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1992 {c |}{res}         0         42 {txt}{c |}{res}        42 
{txt}      1994 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1996 {c |}{res}        42          0 {txt}{c |}{res}        42 
{txt}      2000 {c |}{res}        42          0 {txt}{c |}{res}        42 
{txt}      2004 {c |}{res}         0         42 {txt}{c |}{res}        42 
{txt}      2008 {c |}{res}        42          0 {txt}{c |}{res}        42 
{txt}      2012 {c |}{res}         0         42 {txt}{c |}{res}        42 
{txt}      2016 {c |}{res}        42          0 {txt}{c |}{res}        42 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       355        173 {txt}{c |}{res}       528 
39

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      1970 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      1972 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      1974 {c |}{res}         0         50 {txt}{c |}{res}        50 
{txt}      1976 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      1978 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      1980 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      1983 {c |}{res}         0         50 {txt}{c |}{res}        50 
{txt}      1984 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      1986 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      1988 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      1990 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      1992 {c |}{res}         0         50 {txt}{c |}{res}        50 
{txt}      1994 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      1996 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      1998 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      2000 {c |}{res}        50          0 {txt}{c |}{res}        50 
{txt}      2002 {c |}{res}         0         38 {txt}{c |}{res}        38 
{txt}      2004 {c |}{res}        26         12 {txt}{c |}{res}        38 
{txt}      2006 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}      2008 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}      2010 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}      2012 {c |}{res}         0         38 {txt}{c |}{res}        38 
{txt}      2014 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}      2016 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       916        238 {txt}{c |}{res}     1,154 
40

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      1972 {c |}{res}         9         37 {txt}{c |}{res}        46 
{txt}      1976 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      1978 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1980 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      1984 {c |}{res}         1         45 {txt}{c |}{res}        46 
{txt}      1988 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      1990 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1992 {c |}{res}         0         46 {txt}{c |}{res}        46 
{txt}      1996 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      2000 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      2004 {c |}{res}         0         46 {txt}{c |}{res}        46 
{txt}      2008 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      2012 {c |}{res}         2         44 {txt}{c |}{res}        46 
{txt}      2014 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      2016 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       383        218 {txt}{c |}{res}       601 
46

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1971 {c |}{res}        37          1 {txt}{c |}{res}        38 
{txt}      1973 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1975 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}      1979 {c |}{res}        38          0 {txt}{c |}{res}        38 
{txt}      1983 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      1987 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1991 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      1995 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1999 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      2003 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      2007 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      2011 {c |}{res}         1         39 {txt}{c |}{res}        40 
{txt}      2015 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       315        160 {txt}{c |}{res}       475 
{txt}
{com}. *The above implies the following will work.  
. replace lagyearmin=1982 if sen==1&sid==1&year==1983
{txt}(35 real changes made)

{com}. replace lagyearmin=1991 if sen==1&sid==24&year==1992
{txt}(51 real changes made)

{com}. replace lagyearmin=1980 if sen==1&sid==39&year==1983
{txt}(50 real changes made)

{com}. *THESE2
. gen these2=sen==1&lagyearmin==.&(sid==23|sid==30|sid==39)
{txt}
{com}. *THESE4
. gen these4=sen==1&lagyearmin==.&(sid==1|sid==18|sid==22|sid==24|sid==46)
{txt}
{com}. *sid=16
. replace these4=1 if sen==1&lagyearmin==.&sid==16&(year==1972|year==1992|year==2004)
{txt}(114 real changes made)

{com}. *sid=31
. replace these4=1 if sen==1&lagyearmin==.&sid==31&(year==1984|year==2004|year==2012)
{txt}(125 real changes made)

{com}. *sid=40
. replace these4=1 if sen==1&lagyearmin==.&sid==40&(year==1972|year==1984|year==2004|year==2012)
{txt}(172 real changes made)

{com}. *FILLIN
. replace lagyearmin=year-2 if these2==1
{txt}(791 real changes made)

{com}. replace lagyearmin=year-4 if these4==1
{txt}(1,346 real changes made)

{com}. replace lagyearmax=lagyearmin if lagyearmax==.
{txt}(2,273 real changes made)

{com}. drop these4 these2
{txt}
{com}. *Again
. *look at sids again
. tab sid if firstcase==0&lagyearmin==.&sen==1

  {txt}number of {c |}
   state in {c |}
 alpha list {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          2 {c |}{res}        127        2.58        2.58
{txt}          4 {c |}{res}        177        3.60        6.18
{txt}          6 {c |}{res}        173        3.52        9.70
{txt}          8 {c |}{res}        105        2.13       11.83
{txt}          9 {c |}{res}        220        4.47       16.30
{txt}         11 {c |}{res}        115        2.34       18.64
{txt}         13 {c |}{res}        295        6.00       24.63
{txt}         14 {c |}{res}        249        5.06       29.70
{txt}         15 {c |}{res}        252        5.12       34.82
{txt}         16 {c |}{res}         78        1.59       36.40
{txt}         17 {c |}{res}        213        4.33       40.73
{txt}         25 {c |}{res}        167        3.39       44.13
{txt}         26 {c |}{res}        265        5.39       49.51
{txt}         28 {c |}{res}         97        1.97       51.48
{txt}         31 {c |}{res}         48        0.98       52.46
{txt}         34 {c |}{res}        269        5.47       57.93
{txt}         35 {c |}{res}        161        3.27       61.20
{txt}         36 {c |}{res}        239        4.86       66.06
{txt}         37 {c |}{res}        147        2.99       69.04
{txt}         38 {c |}{res}        246        5.00       74.04
{txt}         40 {c |}{res}         46        0.93       74.98
{txt}         42 {c |}{res}        197        4.00       78.98
{txt}         43 {c |}{res}        204        4.15       83.13
{txt}         44 {c |}{res}        145        2.95       86.08
{txt}         47 {c |}{res}        252        5.12       91.20
{txt}         48 {c |}{res}        151        3.07       94.27
{txt}         49 {c |}{res}        178        3.62       97.89
{txt}         50 {c |}{res}        104        2.11      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      4,920      100.00
{txt}
{com}. *some of those state senates had all their seats up in years ending in two.  They probably aren't missing lagyearmin, though.  
. drop miss
{txt}
{com}. gen miss=lagyearmin==.&firstcase==0
{txt}
{com}. foreach num in 2 4 6 8 9 11 13 14 15 16 17 25 26 28 31 34 35 36 37 38 40 42 43 44 47 48 49 50 {c -(}
{txt}  2{com}. di "`num'"
{txt}  3{com}. tab year miss if sen==1&sid==`num'
{txt}  4{com}. {c )-}
2

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}         7          0 {txt}{c |}{res}         7 
{txt}      1970 {c |}{res}         6          0 {txt}{c |}{res}         6 
{txt}      1972 {c |}{res}         0          7 {txt}{c |}{res}         7 
{txt}      1974 {c |}{res}         0         11 {txt}{c |}{res}        11 
{txt}      1976 {c |}{res}         5          5 {txt}{c |}{res}        10 
{txt}      1978 {c |}{res}         9          0 {txt}{c |}{res}         9 
{txt}      1980 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      1982 {c |}{res}         0         18 {txt}{c |}{res}        18 
{txt}      1984 {c |}{res}         1         10 {txt}{c |}{res}        11 
{txt}      1986 {c |}{res}         5          5 {txt}{c |}{res}        10 
{txt}      1988 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      1990 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      1992 {c |}{res}         0         20 {txt}{c |}{res}        20 
{txt}      1994 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      1996 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      1998 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      2000 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      2002 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      2004 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      2006 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      2008 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      2010 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      2012 {c |}{res}         0         19 {txt}{c |}{res}        19 
{txt}      2014 {c |}{res}         3         11 {txt}{c |}{res}        14 
{txt}      2016 {c |}{res}         6          4 {txt}{c |}{res}        10 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       154        127 {txt}{c |}{res}       281 
4

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}         8          0 {txt}{c |}{res}         8 
{txt}      1970 {c |}{res}        27          0 {txt}{c |}{res}        27 
{txt}      1972 {c |}{res}         2         33 {txt}{c |}{res}        35 
{txt}      1974 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      1976 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1978 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      1980 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      1982 {c |}{res}         0         35 {txt}{c |}{res}        35 
{txt}      1984 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      1986 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1988 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      1990 {c |}{res}        14          4 {txt}{c |}{res}        18 
{txt}      1992 {c |}{res}         0         35 {txt}{c |}{res}        35 
{txt}      1994 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      1996 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1998 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      2000 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2002 {c |}{res}         0         35 {txt}{c |}{res}        35 
{txt}      2004 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      2006 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2008 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      2010 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2012 {c |}{res}         0         35 {txt}{c |}{res}        35 
{txt}      2014 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      2016 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       352        177 {txt}{c |}{res}       529 
6

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      1970 {c |}{res}        16          1 {txt}{c |}{res}        17 
{txt}      1972 {c |}{res}         1         17 {txt}{c |}{res}        18 
{txt}      1974 {c |}{res}         1         17 {txt}{c |}{res}        18 
{txt}      1976 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      1978 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1980 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      1982 {c |}{res}         0         18 {txt}{c |}{res}        18 
{txt}      1984 {c |}{res}         2         17 {txt}{c |}{res}        19 
{txt}      1986 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1988 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      1990 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1992 {c |}{res}         0         18 {txt}{c |}{res}        18 
{txt}      1994 {c |}{res}         1         16 {txt}{c |}{res}        17 
{txt}      1996 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      1998 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2000 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      2002 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      2004 {c |}{res}         0         18 {txt}{c |}{res}        18 
{txt}      2006 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      2008 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      2010 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      2012 {c |}{res}         0         20 {txt}{c |}{res}        20 
{txt}      2014 {c |}{res}         4         14 {txt}{c |}{res}        18 
{txt}      2016 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       278        173 {txt}{c |}{res}       451 
8

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      1970 {c |}{res}         9          0 {txt}{c |}{res}         9 
{txt}      1972 {c |}{res}         0         21 {txt}{c |}{res}        21 
{txt}      1974 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      1976 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      1978 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      1980 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      1982 {c |}{res}         0         21 {txt}{c |}{res}        21 
{txt}      1984 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      1986 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      1988 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      1990 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      1992 {c |}{res}         0         21 {txt}{c |}{res}        21 
{txt}      1994 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      1996 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      1998 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      2000 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      2002 {c |}{res}         0         21 {txt}{c |}{res}        21 
{txt}      2004 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      2006 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      2008 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      2010 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      2012 {c |}{res}         0         21 {txt}{c |}{res}        21 
{txt}      2014 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      2016 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       217        105 {txt}{c |}{res}       322 
9

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        48          0 {txt}{c |}{res}        48 
{txt}      1970 {c |}{res}         9          0 {txt}{c |}{res}         9 
{txt}      1972 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      1974 {c |}{res}        22          0 {txt}{c |}{res}        22 
{txt}      1976 {c |}{res}        20          0 {txt}{c |}{res}        20 
{txt}      1978 {c |}{res}        23          0 {txt}{c |}{res}        23 
{txt}      1980 {c |}{res}        21          0 {txt}{c |}{res}        21 
{txt}      1982 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      1984 {c |}{res}        20          0 {txt}{c |}{res}        20 
{txt}      1986 {c |}{res}        22          0 {txt}{c |}{res}        22 
{txt}      1988 {c |}{res}        22          0 {txt}{c |}{res}        22 
{txt}      1990 {c |}{res}        21          0 {txt}{c |}{res}        21 
{txt}      1992 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      1994 {c |}{res}        22          0 {txt}{c |}{res}        22 
{txt}      1996 {c |}{res}        21          0 {txt}{c |}{res}        21 
{txt}      1998 {c |}{res}        20          0 {txt}{c |}{res}        20 
{txt}      2000 {c |}{res}        20          0 {txt}{c |}{res}        20 
{txt}      2002 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      2004 {c |}{res}        22          0 {txt}{c |}{res}        22 
{txt}      2006 {c |}{res}        20          0 {txt}{c |}{res}        20 
{txt}      2008 {c |}{res}        21          0 {txt}{c |}{res}        21 
{txt}      2010 {c |}{res}        23          0 {txt}{c |}{res}        23 
{txt}      2012 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      2014 {c |}{res}        20          0 {txt}{c |}{res}        20 
{txt}      2016 {c |}{res}        20         20 {txt}{c |}{res}        40 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       437        220 {txt}{c |}{res}       657 
11

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}         7          0 {txt}{c |}{res}         7 
{txt}      1970 {c |}{res}         8          0 {txt}{c |}{res}         8 
{txt}      1974 {c |}{res}         3          5 {txt}{c |}{res}         8 
{txt}      1976 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1978 {c |}{res}         8          0 {txt}{c |}{res}         8 
{txt}      1980 {c |}{res}         7          0 {txt}{c |}{res}         7 
{txt}      1982 {c |}{res}         1         12 {txt}{c |}{res}        13 
{txt}      1984 {c |}{res}         0         13 {txt}{c |}{res}        13 
{txt}      1986 {c |}{res}         0         12 {txt}{c |}{res}        12 
{txt}      1988 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1990 {c |}{res}        12          0 {txt}{c |}{res}        12 
{txt}      1992 {c |}{res}         0         13 {txt}{c |}{res}        13 
{txt}      1994 {c |}{res}         2         12 {txt}{c |}{res}        14 
{txt}      1996 {c |}{res}        13          0 {txt}{c |}{res}        13 
{txt}      1998 {c |}{res}        13          0 {txt}{c |}{res}        13 
{txt}      2000 {c |}{res}        14          0 {txt}{c |}{res}        14 
{txt}      2002 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      2004 {c |}{res}        12          0 {txt}{c |}{res}        12 
{txt}      2006 {c |}{res}        13          0 {txt}{c |}{res}        13 
{txt}      2008 {c |}{res}        12          0 {txt}{c |}{res}        12 
{txt}      2010 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2012 {c |}{res}         2         23 {txt}{c |}{res}        25 
{txt}      2014 {c |}{res}        13          0 {txt}{c |}{res}        13 
{txt}      2016 {c |}{res}        14          0 {txt}{c |}{res}        14 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       185        115 {txt}{c |}{res}       300 
13

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      1970 {c |}{res}        58          0 {txt}{c |}{res}        58 
{txt}      1972 {c |}{res}         0         59 {txt}{c |}{res}        59 
{txt}      1974 {c |}{res}        20          0 {txt}{c |}{res}        20 
{txt}      1976 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1978 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1980 {c |}{res}        20          0 {txt}{c |}{res}        20 
{txt}      1982 {c |}{res}         0         59 {txt}{c |}{res}        59 
{txt}      1984 {c |}{res}        21          0 {txt}{c |}{res}        21 
{txt}      1986 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1988 {c |}{res}        39          0 {txt}{c |}{res}        39 
{txt}      1990 {c |}{res}        20          0 {txt}{c |}{res}        20 
{txt}      1992 {c |}{res}         0         59 {txt}{c |}{res}        59 
{txt}      1994 {c |}{res}        20          0 {txt}{c |}{res}        20 
{txt}      1996 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1998 {c |}{res}        41          0 {txt}{c |}{res}        41 
{txt}      2000 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      2002 {c |}{res}         0         59 {txt}{c |}{res}        59 
{txt}      2004 {c |}{res}        23          0 {txt}{c |}{res}        23 
{txt}      2006 {c |}{res}        39          0 {txt}{c |}{res}        39 
{txt}      2008 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      2010 {c |}{res}        21          0 {txt}{c |}{res}        21 
{txt}      2012 {c |}{res}         0         59 {txt}{c |}{res}        59 
{txt}      2014 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      2016 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       602        295 {txt}{c |}{res}       897 
14

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      1970 {c |}{res}        22          0 {txt}{c |}{res}        22 
{txt}      1972 {c |}{res}         0         26 {txt}{c |}{res}        26 
{txt}      1974 {c |}{res}         1         24 {txt}{c |}{res}        25 
{txt}      1976 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1978 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1980 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1982 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      1984 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      1986 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1988 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1990 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1992 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      1994 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      1996 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1998 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2000 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2002 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      2004 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      2006 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2008 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2010 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2012 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      2014 {c |}{res}         1         24 {txt}{c |}{res}        25 
{txt}      2016 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       367        249 {txt}{c |}{res}       616 
15

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        33          0 {txt}{c |}{res}        33 
{txt}      1970 {c |}{res}        24          3 {txt}{c |}{res}        27 
{txt}      1972 {c |}{res}         0         50 {txt}{c |}{res}        50 
{txt}      1974 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1976 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1978 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1980 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1982 {c |}{res}         0         29 {txt}{c |}{res}        29 
{txt}      1984 {c |}{res}         4         21 {txt}{c |}{res}        25 
{txt}      1986 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1988 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1990 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1992 {c |}{res}         0         32 {txt}{c |}{res}        32 
{txt}      1994 {c |}{res}         7         18 {txt}{c |}{res}        25 
{txt}      1996 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1998 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2000 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2002 {c |}{res}         0         35 {txt}{c |}{res}        35 
{txt}      2004 {c |}{res}        10         15 {txt}{c |}{res}        25 
{txt}      2006 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2008 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2010 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2012 {c |}{res}         1         25 {txt}{c |}{res}        26 
{txt}      2014 {c |}{res}         1         24 {txt}{c |}{res}        25 
{txt}      2016 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       430        252 {txt}{c |}{res}       682 
16

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1972 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1974 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1976 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1978 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1980 {c |}{res}         2         38 {txt}{c |}{res}        40 
{txt}      1984 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1988 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1992 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1996 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      1998 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      2000 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      2004 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      2008 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}      2010 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      2012 {c |}{res}         0         40 {txt}{c |}{res}        40 
{txt}      2014 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      2016 {c |}{res}        40          0 {txt}{c |}{res}        40 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       450         78 {txt}{c |}{res}       528 
17

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1969 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      1971 {c |}{res}        16          3 {txt}{c |}{res}        19 
{txt}      1973 {c |}{res}         0         19 {txt}{c |}{res}        19 
{txt}      1975 {c |}{res}         3         16 {txt}{c |}{res}        19 
{txt}      1977 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      1979 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      1981 {c |}{res}        21          0 {txt}{c |}{res}        21 
{txt}      1983 {c |}{res}         0         19 {txt}{c |}{res}        19 
{txt}      1986 {c |}{res}         2         18 {txt}{c |}{res}        20 
{txt}      1988 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      1990 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      1992 {c |}{res}         0         19 {txt}{c |}{res}        19 
{txt}      1994 {c |}{res}         2         17 {txt}{c |}{res}        19 
{txt}      1996 {c |}{res}         3         16 {txt}{c |}{res}        19 
{txt}      1998 {c |}{res}         5         14 {txt}{c |}{res}        19 
{txt}      2000 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      2002 {c |}{res}         2         17 {txt}{c |}{res}        19 
{txt}      2004 {c |}{res}         2         17 {txt}{c |}{res}        19 
{txt}      2006 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      2008 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      2010 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      2012 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      2014 {c |}{res}         0         19 {txt}{c |}{res}        19 
{txt}      2016 {c |}{res}         0         19 {txt}{c |}{res}        19 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       246        213 {txt}{c |}{res}       459 
25

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1970 {c |}{res}        16          1 {txt}{c |}{res}        17 
{txt}      1972 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      1974 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      1976 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1978 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1980 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1982 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      1984 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      1986 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1988 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1990 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      1992 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      1994 {c |}{res}         1         16 {txt}{c |}{res}        17 
{txt}      1996 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1998 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      2000 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2002 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      2004 {c |}{res}         1         16 {txt}{c |}{res}        17 
{txt}      2006 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2008 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2010 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2012 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      2014 {c |}{res}         2         15 {txt}{c |}{res}        17 
{txt}      2016 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       260        167 {txt}{c |}{res}       427 
26

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        21          0 {txt}{c |}{res}        21 
{txt}      1970 {c |}{res}        21          0 {txt}{c |}{res}        21 
{txt}      1972 {c |}{res}         0         20 {txt}{c |}{res}        20 
{txt}      1974 {c |}{res}         3         47 {txt}{c |}{res}        50 
{txt}      1976 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1978 {c |}{res}        27          0 {txt}{c |}{res}        27 
{txt}      1980 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1982 {c |}{res}        26          0 {txt}{c |}{res}        26 
{txt}      1984 {c |}{res}         1         24 {txt}{c |}{res}        25 
{txt}      1986 {c |}{res}         1         24 {txt}{c |}{res}        25 
{txt}      1988 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1990 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1992 {c |}{res}        26          0 {txt}{c |}{res}        26 
{txt}      1994 {c |}{res}         0         28 {txt}{c |}{res}        28 
{txt}      1996 {c |}{res}         3         22 {txt}{c |}{res}        25 
{txt}      1998 {c |}{res}        26          0 {txt}{c |}{res}        26 
{txt}      2000 {c |}{res}        26          0 {txt}{c |}{res}        26 
{txt}      2002 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2004 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      2006 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      2008 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2010 {c |}{res}        26          0 {txt}{c |}{res}        26 
{txt}      2012 {c |}{res}        26          0 {txt}{c |}{res}        26 
{txt}      2014 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      2016 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       383        265 {txt}{c |}{res}       648 
28

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}         8          0 {txt}{c |}{res}         8 
{txt}      1970 {c |}{res}         7          0 {txt}{c |}{res}         7 
{txt}      1972 {c |}{res}         0          6 {txt}{c |}{res}         6 
{txt}      1974 {c |}{res}         1          6 {txt}{c |}{res}         7 
{txt}      1976 {c |}{res}         5          1 {txt}{c |}{res}         6 
{txt}      1978 {c |}{res}         7          0 {txt}{c |}{res}         7 
{txt}      1980 {c |}{res}         6          0 {txt}{c |}{res}         6 
{txt}      1982 {c |}{res}         0         11 {txt}{c |}{res}        11 
{txt}      1984 {c |}{res}         0         10 {txt}{c |}{res}        10 
{txt}      1986 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      1988 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      1990 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      1992 {c |}{res}         0         12 {txt}{c |}{res}        12 
{txt}      1994 {c |}{res}         2          9 {txt}{c |}{res}        11 
{txt}      1996 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      1998 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      2000 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      2002 {c |}{res}         0         11 {txt}{c |}{res}        11 
{txt}      2004 {c |}{res}         0         10 {txt}{c |}{res}        10 
{txt}      2006 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      2008 {c |}{res}        10          0 {txt}{c |}{res}        10 
{txt}      2010 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      2012 {c |}{res}         0         12 {txt}{c |}{res}        12 
{txt}      2014 {c |}{res}         2          9 {txt}{c |}{res}        11 
{txt}      2016 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       145         97 {txt}{c |}{res}       242 
31

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1970 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      1972 {c |}{res}        28          6 {txt}{c |}{res}        34 
{txt}      1974 {c |}{res}         8          0 {txt}{c |}{res}         8 
{txt}      1976 {c |}{res}        35          0 {txt}{c |}{res}        35 
{txt}      1978 {c |}{res}         9          0 {txt}{c |}{res}         9 
{txt}      1980 {c |}{res}        36          0 {txt}{c |}{res}        36 
{txt}      1984 {c |}{res}        42          0 {txt}{c |}{res}        42 
{txt}      1986 {c |}{res}         2          0 {txt}{c |}{res}         2 
{txt}      1988 {c |}{res}        42          0 {txt}{c |}{res}        42 
{txt}      1990 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1992 {c |}{res}         0         42 {txt}{c |}{res}        42 
{txt}      1994 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1996 {c |}{res}        42          0 {txt}{c |}{res}        42 
{txt}      2000 {c |}{res}        42          0 {txt}{c |}{res}        42 
{txt}      2004 {c |}{res}        42          0 {txt}{c |}{res}        42 
{txt}      2008 {c |}{res}        42          0 {txt}{c |}{res}        42 
{txt}      2012 {c |}{res}        42          0 {txt}{c |}{res}        42 
{txt}      2016 {c |}{res}        42          0 {txt}{c |}{res}        42 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       480         48 {txt}{c |}{res}       528 
34

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        20          0 {txt}{c |}{res}        20 
{txt}      1970 {c |}{res}        21          0 {txt}{c |}{res}        21 
{txt}      1972 {c |}{res}         3         22 {txt}{c |}{res}        25 
{txt}      1974 {c |}{res}         7         12 {txt}{c |}{res}        19 
{txt}      1976 {c |}{res}         3         36 {txt}{c |}{res}        39 
{txt}      1978 {c |}{res}        15         10 {txt}{c |}{res}        25 
{txt}      1980 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      1982 {c |}{res}         0         35 {txt}{c |}{res}        35 
{txt}      1984 {c |}{res}        11         16 {txt}{c |}{res}        27 
{txt}      1986 {c |}{res}        27          0 {txt}{c |}{res}        27 
{txt}      1988 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1990 {c |}{res}        27          0 {txt}{c |}{res}        27 
{txt}      1992 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      1994 {c |}{res}         2         23 {txt}{c |}{res}        25 
{txt}      1996 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      1998 {c |}{res}        26          0 {txt}{c |}{res}        26 
{txt}      2000 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2002 {c |}{res}         1         25 {txt}{c |}{res}        26 
{txt}      2004 {c |}{res}         2         21 {txt}{c |}{res}        23 
{txt}      2006 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      2008 {c |}{res}        23          0 {txt}{c |}{res}        23 
{txt}      2010 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      2012 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      2014 {c |}{res}         5         19 {txt}{c |}{res}        24 
{txt}      2016 {c |}{res}        23          0 {txt}{c |}{res}        23 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       362        269 {txt}{c |}{res}       631 
35

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1970 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1972 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      1974 {c |}{res}         2         16 {txt}{c |}{res}        18 
{txt}      1976 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1978 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1980 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1982 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      1984 {c |}{res}         1         16 {txt}{c |}{res}        17 
{txt}      1986 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      1988 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1990 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1992 {c |}{res}         2         15 {txt}{c |}{res}        17 
{txt}      1994 {c |}{res}         1         16 {txt}{c |}{res}        17 
{txt}      1996 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1998 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      2000 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2002 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      2004 {c |}{res}         1         15 {txt}{c |}{res}        16 
{txt}      2006 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2008 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      2010 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2012 {c |}{res}         1         17 {txt}{c |}{res}        18 
{txt}      2014 {c |}{res}         2         15 {txt}{c |}{res}        17 
{txt}      2016 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       260        161 {txt}{c |}{res}       421 
36

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      1970 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      1972 {c |}{res}         1         26 {txt}{c |}{res}        27 
{txt}      1974 {c |}{res}         4         21 {txt}{c |}{res}        25 
{txt}      1976 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      1978 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      1980 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      1982 {c |}{res}         0         27 {txt}{c |}{res}        27 
{txt}      1984 {c |}{res}         3         21 {txt}{c |}{res}        24 
{txt}      1986 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1988 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      1990 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      1992 {c |}{res}         0         24 {txt}{c |}{res}        24 
{txt}      1994 {c |}{res}         0         24 {txt}{c |}{res}        24 
{txt}      1996 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      1998 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      2000 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      2002 {c |}{res}         0         24 {txt}{c |}{res}        24 
{txt}      2004 {c |}{res}         1         24 {txt}{c |}{res}        25 
{txt}      2006 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      2008 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      2010 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      2012 {c |}{res}         0         24 {txt}{c |}{res}        24 
{txt}      2014 {c |}{res}         1         24 {txt}{c |}{res}        25 
{txt}      2016 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       372        239 {txt}{c |}{res}       611 
37

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1970 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1972 {c |}{res}         2         13 {txt}{c |}{res}        15 
{txt}      1974 {c |}{res}         0         15 {txt}{c |}{res}        15 
{txt}      1976 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1978 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1980 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1982 {c |}{res}         0         15 {txt}{c |}{res}        15 
{txt}      1984 {c |}{res}         1         14 {txt}{c |}{res}        15 
{txt}      1986 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1988 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1990 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1992 {c |}{res}         0         16 {txt}{c |}{res}        16 
{txt}      1994 {c |}{res}         1         14 {txt}{c |}{res}        15 
{txt}      1996 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1998 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2000 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2002 {c |}{res}         0         15 {txt}{c |}{res}        15 
{txt}      2004 {c |}{res}         2         15 {txt}{c |}{res}        17 
{txt}      2006 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2008 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      2010 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      2012 {c |}{res}         0         16 {txt}{c |}{res}        16 
{txt}      2014 {c |}{res}         2         14 {txt}{c |}{res}        16 
{txt}      2016 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       242        147 {txt}{c |}{res}       389 
38

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1970 {c |}{res}        26          0 {txt}{c |}{res}        26 
{txt}      1972 {c |}{res}         2         23 {txt}{c |}{res}        25 
{txt}      1974 {c |}{res}         1         24 {txt}{c |}{res}        25 
{txt}      1976 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1978 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1980 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1982 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      1984 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      1986 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1988 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1990 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1992 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      1994 {c |}{res}         1         24 {txt}{c |}{res}        25 
{txt}      1996 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1998 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2000 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2002 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      2004 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      2006 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2008 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2010 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2012 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2014 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}      2016 {c |}{res}         0         25 {txt}{c |}{res}        25 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       380        246 {txt}{c |}{res}       626 
40

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      1972 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      1976 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      1978 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1980 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      1984 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      1988 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      1990 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      1992 {c |}{res}         0         46 {txt}{c |}{res}        46 
{txt}      1996 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      2000 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      2004 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      2008 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      2012 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}      2014 {c |}{res}         1          0 {txt}{c |}{res}         1 
{txt}      2016 {c |}{res}        46          0 {txt}{c |}{res}        46 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       555         46 {txt}{c |}{res}       601 
42

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        33          0 {txt}{c |}{res}        33 
{txt}      1970 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      1972 {c |}{res}         1         15 {txt}{c |}{res}        16 
{txt}      1974 {c |}{res}         0         19 {txt}{c |}{res}        19 
{txt}      1976 {c |}{res}         5         12 {txt}{c |}{res}        17 
{txt}      1978 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1980 {c |}{res}         0         16 {txt}{c |}{res}        16 
{txt}      1982 {c |}{res}         0         18 {txt}{c |}{res}        18 
{txt}      1984 {c |}{res}         1         16 {txt}{c |}{res}        17 
{txt}      1986 {c |}{res}         6         11 {txt}{c |}{res}        17 
{txt}      1988 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1990 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1992 {c |}{res}         2         14 {txt}{c |}{res}        16 
{txt}      1994 {c |}{res}         1         16 {txt}{c |}{res}        17 
{txt}      1996 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1998 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2000 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2002 {c |}{res}         2         15 {txt}{c |}{res}        17 
{txt}      2004 {c |}{res}         3         13 {txt}{c |}{res}        16 
{txt}      2006 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2008 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      2010 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2012 {c |}{res}         1         15 {txt}{c |}{res}        16 
{txt}      2014 {c |}{res}         1         17 {txt}{c |}{res}        18 
{txt}      2016 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       240        197 {txt}{c |}{res}       437 
43

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1970 {c |}{res}        15          1 {txt}{c |}{res}        16 
{txt}      1972 {c |}{res}         0         31 {txt}{c |}{res}        31 
{txt}      1974 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1976 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1978 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1980 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1982 {c |}{res}         0         31 {txt}{c |}{res}        31 
{txt}      1984 {c |}{res}        14          1 {txt}{c |}{res}        15 
{txt}      1986 {c |}{res}         9          7 {txt}{c |}{res}        16 
{txt}      1988 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1990 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1992 {c |}{res}         0         31 {txt}{c |}{res}        31 
{txt}      1994 {c |}{res}         0         31 {txt}{c |}{res}        31 
{txt}      1996 {c |}{res}         9          6 {txt}{c |}{res}        15 
{txt}      1998 {c |}{res}        10          6 {txt}{c |}{res}        16 
{txt}      2000 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2002 {c |}{res}         0         31 {txt}{c |}{res}        31 
{txt}      2004 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2006 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      2008 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2010 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      2012 {c |}{res}         3         28 {txt}{c |}{res}        31 
{txt}      2014 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2016 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       276        204 {txt}{c |}{res}       480 
44

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        14          0 {txt}{c |}{res}        14 
{txt}      1970 {c |}{res}        14          0 {txt}{c |}{res}        14 
{txt}      1972 {c |}{res}         0         29 {txt}{c |}{res}        29 
{txt}      1974 {c |}{res}        14          0 {txt}{c |}{res}        14 
{txt}      1976 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1978 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1980 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1982 {c |}{res}         0         18 {txt}{c |}{res}        18 
{txt}      1984 {c |}{res}         3         11 {txt}{c |}{res}        14 
{txt}      1986 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1988 {c |}{res}        14          0 {txt}{c |}{res}        14 
{txt}      1990 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1992 {c |}{res}         0         16 {txt}{c |}{res}        16 
{txt}      1994 {c |}{res}         3         13 {txt}{c |}{res}        16 
{txt}      1996 {c |}{res}        14          0 {txt}{c |}{res}        14 
{txt}      1998 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2000 {c |}{res}        14          0 {txt}{c |}{res}        14 
{txt}      2002 {c |}{res}         0         16 {txt}{c |}{res}        16 
{txt}      2004 {c |}{res}         2         13 {txt}{c |}{res}        15 
{txt}      2006 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      2008 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2010 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2012 {c |}{res}         0         16 {txt}{c |}{res}        16 
{txt}      2014 {c |}{res}         1         13 {txt}{c |}{res}        14 
{txt}      2016 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       244        145 {txt}{c |}{res}       389 
47

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        26          0 {txt}{c |}{res}        26 
{txt}      1970 {c |}{res}        23          1 {txt}{c |}{res}        24 
{txt}      1972 {c |}{res}         0         26 {txt}{c |}{res}        26 
{txt}      1974 {c |}{res}         1         23 {txt}{c |}{res}        24 
{txt}      1976 {c |}{res}        19          6 {txt}{c |}{res}        25 
{txt}      1978 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1980 {c |}{res}        26          0 {txt}{c |}{res}        26 
{txt}      1982 {c |}{res}         0         24 {txt}{c |}{res}        24 
{txt}      1984 {c |}{res}         1         25 {txt}{c |}{res}        26 
{txt}      1986 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      1988 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1990 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1992 {c |}{res}         0         26 {txt}{c |}{res}        26 
{txt}      1994 {c |}{res}         1         23 {txt}{c |}{res}        24 
{txt}      1996 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      1998 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      2000 {c |}{res}        26          0 {txt}{c |}{res}        26 
{txt}      2002 {c |}{res}         0         24 {txt}{c |}{res}        24 
{txt}      2004 {c |}{res}         3         25 {txt}{c |}{res}        28 
{txt}      2006 {c |}{res}        24          0 {txt}{c |}{res}        24 
{txt}      2008 {c |}{res}        26          0 {txt}{c |}{res}        26 
{txt}      2010 {c |}{res}        25          0 {txt}{c |}{res}        25 
{txt}      2012 {c |}{res}         0         26 {txt}{c |}{res}        26 
{txt}      2014 {c |}{res}         2         23 {txt}{c |}{res}        25 
{txt}      2016 {c |}{res}        26          0 {txt}{c |}{res}        26 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       377        252 {txt}{c |}{res}       629 
48

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1970 {c |}{res}        14          3 {txt}{c |}{res}        17 
{txt}      1972 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      1974 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1976 {c |}{res}         4         14 {txt}{c |}{res}        18 
{txt}      1978 {c |}{res}         5         12 {txt}{c |}{res}        17 
{txt}      1980 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1982 {c |}{res}         2         15 {txt}{c |}{res}        17 
{txt}      1984 {c |}{res}         2         15 {txt}{c |}{res}        17 
{txt}      1986 {c |}{res}        19          0 {txt}{c |}{res}        19 
{txt}      1988 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1990 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1992 {c |}{res}         2         15 {txt}{c |}{res}        17 
{txt}      1994 {c |}{res}         2         15 {txt}{c |}{res}        17 
{txt}      1996 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1998 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2000 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      2002 {c |}{res}         3         14 {txt}{c |}{res}        17 
{txt}      2004 {c |}{res}         3         14 {txt}{c |}{res}        17 
{txt}      2006 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2008 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2010 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      2012 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      2014 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      2016 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       282        151 {txt}{c |}{res}       433 
49

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1970 {c |}{res}        16          1 {txt}{c |}{res}        17 
{txt}      1972 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      1974 {c |}{res}         1         16 {txt}{c |}{res}        17 
{txt}      1976 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1978 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1980 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1982 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      1984 {c |}{res}         1         16 {txt}{c |}{res}        17 
{txt}      1986 {c |}{res}         4         13 {txt}{c |}{res}        17 
{txt}      1988 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1990 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      1992 {c |}{res}         0         16 {txt}{c |}{res}        16 
{txt}      1994 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      1996 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      1998 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2000 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      2002 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      2004 {c |}{res}         0         16 {txt}{c |}{res}        16 
{txt}      2006 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2008 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      2010 {c |}{res}        17          0 {txt}{c |}{res}        17 
{txt}      2012 {c |}{res}         1         15 {txt}{c |}{res}        16 
{txt}      2014 {c |}{res}         0         17 {txt}{c |}{res}        17 
{txt}      2016 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       238        178 {txt}{c |}{res}       416 
50

           {txt}{c |}         miss
      year {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      1968 {c |}{res}        12          0 {txt}{c |}{res}        12 
{txt}      1970 {c |}{res}        12          0 {txt}{c |}{res}        12 
{txt}      1972 {c |}{res}         8          5 {txt}{c |}{res}        13 
{txt}      1974 {c |}{res}        12          1 {txt}{c |}{res}        13 
{txt}      1976 {c |}{res}        11          0 {txt}{c |}{res}        11 
{txt}      1978 {c |}{res}        13          0 {txt}{c |}{res}        13 
{txt}      1980 {c |}{res}        12          0 {txt}{c |}{res}        12 
{txt}      1982 {c |}{res}         9          3 {txt}{c |}{res}        12 
{txt}      1984 {c |}{res}         7          5 {txt}{c |}{res}        12 
{txt}      1986 {c |}{res}        11          1 {txt}{c |}{res}        12 
{txt}      1988 {c |}{res}        12          0 {txt}{c |}{res}        12 
{txt}      1990 {c |}{res}        12          0 {txt}{c |}{res}        12 
{txt}      1992 {c |}{res}         0         30 {txt}{c |}{res}        30 
{txt}      1994 {c |}{res}        18          0 {txt}{c |}{res}        18 
{txt}      1996 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      1998 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2000 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2002 {c |}{res}         0         15 {txt}{c |}{res}        15 
{txt}      2004 {c |}{res}         1         15 {txt}{c |}{res}        16 
{txt}      2006 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2008 {c |}{res}        16          0 {txt}{c |}{res}        16 
{txt}      2010 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}      2012 {c |}{res}         1         14 {txt}{c |}{res}        15 
{txt}      2014 {c |}{res}         0         15 {txt}{c |}{res}        15 
{txt}      2016 {c |}{res}        15          0 {txt}{c |}{res}        15 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       257        104 {txt}{c |}{res}       361 
{txt}
{com}. *None of those can be filled in.  
. drop miss
{txt}
{com}. save $mainfile, replace
{txt}file 102slersuoacontest20181024.dta saved

{com}. 
. *LAGSYSMIS
. *Bring in the codes that indicate that lagged values that were changed to system missing have no variation in the variable in question in the entire chamber-year (or year group, if appropriate) among the possible lagged values.  
. clear
{txt}
{com}. use $mainfile
{txt}
{com}. *are lagyearmin and lagyearmax ever dif when there is redistricting?  I think by virtue of how I filled those in, that wouldn't be possible.  
. *gen lagyeardif=lagyearmin!=lagyearmax
. *tab lagyeardif
. *There are only 5 difs total, that is a surprise, I thought there were way more.  
. *CHECK INTO THE ABOVE SOME OTHER TIME
. assert redist==0 if lagyearmin!=lagyearmax
{txt}
{com}. *Those are all redist=0, that makes things a lot easier.  
. *LAGAID1
. rename lagyearmin yearlagaid
{res}{txt}
{com}. merge m:1 sid sen yearlagaid using templagaid1
{res}
{txt}{col 5}Result{col 38}# of obs.
{col 5}{hline 41}
{col 5}not matched{col 30}{res}          17,879
{txt}{col 9}from master{col 30}{res}          17,810{txt}  (_merge==1)
{col 9}from using{col 30}{res}              69{txt}  (_merge==2)

{col 5}matched{col 30}{res}         122,426{txt}  (_merge==3)
{col 5}{hline 41}

{com}. erase templagaid1.dta
{txt}
{com}. drop if _merge==2
{txt}(69 observations deleted)

{com}. drop _merge
{txt}
{com}. foreach string of global lagaidvars {c -(}
{txt}  2{com}. replace `string'lag=`string'mean if `string'lag==.&`string'novar==1
{txt}  3{com}. drop `string'mean `string'novar
{txt}  4{com}. {c )-}
{txt}(35 real changes made)
(456 real changes made)
(0 real changes made)
(0 real changes made)
(2,205 real changes made)
(23,028 real changes made)
(0 real changes made)
(624 real changes made)
(6,521 real changes made)
(21,074 real changes made)
(22,598 real changes made)
(21,227 real changes made)
(24,351 real changes made)
(24,826 real changes made)
(27,427 real changes made)
(547 real changes made)
(70 real changes made)
(16,700 real changes made)
(25,776 real changes made)

{com}. rename yearlagaid lagyearmin
{res}{txt}
{com}. *LAGAID2
. *Bring lag aid #2 ahead in time two years.  
. gen yearlagaid=year-2
{txt}
{com}. merge m:1 sid sen yearlagaid using templagaid2
{res}
{txt}{col 5}Result{col 38}# of obs.
{col 5}{hline 41}
{col 5}not matched{col 30}{res}          53,534
{txt}{col 9}from master{col 30}{res}          51,774{txt}  (_merge==1)
{col 9}from using{col 30}{res}           1,760{txt}  (_merge==2)

{col 5}matched{col 30}{res}          88,462{txt}  (_merge==3)
{col 5}{hline 41}

{com}. drop if _merge==2
{txt}(1,760 observations deleted)

{com}. drop _merge
{txt}
{com}. foreach string of global lagaidvars {c -(}
{txt}  2{com}. replace `string'lag=`string'mean if `string'lag==.&`string'novar==1
{txt}  3{com}. drop `string'mean `string'novar
{txt}  4{com}. {c )-}
{txt}(0 real changes made)
(96 real changes made)
(0 real changes made)
(21 real changes made)
(236 real changes made)
(581 real changes made)
(0 real changes made)
(0 real changes made)
(818 real changes made)
(2,379 real changes made)
(2,515 real changes made)
(3,054 real changes made)
(3,093 real changes made)
(2,821 real changes made)
(3,027 real changes made)
(129 real changes made)
(35 real changes made)
(2,840 real changes made)
(3,234 real changes made)

{com}. *Bring lag aid #2 ahead in time four years.  
. *Only allow sysmis to be replaced by a value if redist is some type of holdover redistricting (or no redistricting, but that possibility shouldn't be present).  If it is sysmis & not holdover redistricting, then it's an instance of re-redistricting (excepting cases in the beginning of the dataset).  
. replace yearlagaid=year-4
{txt}(140,236 real changes made)

{com}. merge m:1 sid sen yearlagaid using templagaid2
{res}
{txt}{col 5}Result{col 38}# of obs.
{col 5}{hline 41}
{col 5}not matched{col 30}{res}          59,603
{txt}{col 9}from master{col 30}{res}          57,753{txt}  (_merge==1)
{col 9}from using{col 30}{res}           1,850{txt}  (_merge==2)

{col 5}matched{col 30}{res}          82,483{txt}  (_merge==3)
{col 5}{hline 41}

{com}. erase templagaid2.dta
{txt}
{com}. drop if _merge==2
{txt}(1,850 observations deleted)

{com}. drop _merge
{txt}
{com}. foreach string of global lagaidvars {c -(}
{txt}  2{com}. replace `string'lag=`string'mean if `string'lag==.&`string'novar==1&(redist==0|redist>5)
{txt}  3{com}. drop `string'mean `string'novar
{txt}  4{com}. {c )-}
{txt}(0 real changes made)
(48 real changes made)
(0 real changes made)
(0 real changes made)
(153 real changes made)
(109 real changes made)
(0 real changes made)
(1 real change made)
(240 real changes made)
(1,145 real changes made)
(1,225 real changes made)
(1,329 real changes made)
(1,366 real changes made)
(1,333 real changes made)
(1,365 real changes made)
(48 real changes made)
(0 real changes made)
(1,205 real changes made)
(1,388 real changes made)

{com}. *LAGAID3(WVSEN)
. merge m:1 sid sen yearlagaid using templagaid3
{res}
{txt}{col 5}Result{col 38}# of obs.
{col 5}{hline 41}
{col 5}not matched{col 30}{res}         139,976
{txt}{col 9}from master{col 30}{res}         139,975{txt}  (_merge==1)
{col 9}from using{col 30}{res}               1{txt}  (_merge==2)

{col 5}matched{col 30}{res}             261{txt}  (_merge==3)
{col 5}{hline 41}

{com}. erase templagaid3.dta
{txt}
{com}. drop if _merge==2
{txt}(1 observation deleted)

{com}. drop _merge
{txt}
{com}. foreach string of global lagaidvars {c -(}
{txt}  2{com}. replace `string'lag=`string'mean if `string'lag==.&`string'novar==1
{txt}  3{com}. drop `string'mean `string'novar
{txt}  4{com}. {c )-}
{txt}(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(104 real changes made)
(104 real changes made)
(118 real changes made)
(118 real changes made)
(118 real changes made)
(118 real changes made)
(0 real changes made)
(0 real changes made)
(118 real changes made)
(118 real changes made)

{com}. drop yearlagaid
{txt}
{com}. 
. *UNCONTLAG-INTERACTIONS
. *Interactions between uncontlag and inclag, otherlag and pastlag.  
. *The impact of past incumbency on change in vote share will be different depending on whether the last election was contested or not.  
. gen incuncontlag=inclag*uncontlag
{txt}(40,961 missing values generated)

{com}. *Are there cases of an incumbent of one party being the only cand of their party running and being opposed by two cands of the other party?
. tab inclag candlag if eseats==2

           {txt}{c |}                                                  (mean) cand
(mean) inc {c |}        -1        -.5  -.3333333       -.25          0        .25   .3333333         .5   .6666667          1 {c |}     Total
{hline 11}{c +}{hline 110}{c +}{hline 10}
        -1 {c |}{res}       239        205          0          0        729          0          0          0          0          0 {txt}{c |}{res}     1,173 
{txt}       -.5 {c |}{res}       131        169          0          1        633          0          0         16          0          0 {txt}{c |}{res}       950 
{txt} -.3333333 {c |}{res}         0          0          1          0          2          0          0          0          0          0 {txt}{c |}{res}         3 
{txt}         0 {c |}{res}        27         81          0          0        766          1          0        108          0         36 {txt}{c |}{res}     1,019 
{txt}  .3333333 {c |}{res}         0          0          0          0          1          0          1          0          1          0 {txt}{c |}{res}         3 
{txt}        .5 {c |}{res}         0         11          0          0        466          1          0        157          0        156 {txt}{c |}{res}       791 
{txt}  .6666667 {c |}{res}         0          0          0          0          1          0          0          0          0          0 {txt}{c |}{res}         1 
{txt}       .75 {c |}{res}         0          0          0          0          0          0          0          0          0          1 {txt}{c |}{res}         1 
{txt}         1 {c |}{res}         0          0          0          0        560          0          1        210          0        309 {txt}{c |}{res}     1,080 
{txt}{hline 11}{c +}{hline 110}{c +}{hline 10}
     Total {c |}{res}       397        466          1          1      3,158          2          2        491          1        502 {txt}{c |}{res}     5,021 
{txt}
{com}. *there are 27 cases like that, unfortunately not 0, but it's not enough to justify modeling it.  Additionally, it will already be controlled for additively for both Xs (partially contested / incumbency).  
. gen otheruncontlag=otherlag*uncontlag
{txt}(40,672 missing values generated)

{com}. gen pastuncontlag=pastlag*uncontlag
{txt}(40,550 missing values generated)

{com}. tab incuncontlag

{txt}incuncontla {c |}
          g {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
         -1 {c |}{res}     11,152       11.23       11.23
{txt}  -.8571429 {c |}{res}          1        0.00       11.23
{txt}        -.8 {c |}{res}          1        0.00       11.24
{txt}       -.75 {c |}{res}         13        0.01       11.25
{txt}  -.6666667 {c |}{res}         23        0.02       11.27
{txt}        -.5 {c |}{res}        154        0.16       11.43
{txt}  -.3333333 {c |}{res}         27        0.03       11.45
{txt}          0 {c |}{res}     68,929       69.43       80.89
{txt}        .25 {c |}{res}          6        0.01       80.89
{txt}   .3333333 {c |}{res}         43        0.04       80.94
{txt}         .4 {c |}{res}          2        0.00       80.94
{txt}         .5 {c |}{res}        183        0.18       81.12
{txt}         .6 {c |}{res}          1        0.00       81.12
{txt}   .6666667 {c |}{res}         59        0.06       81.18
{txt}        .75 {c |}{res}         24        0.02       81.21
{txt}         .8 {c |}{res}          4        0.00       81.21
{txt}   .8333333 {c |}{res}          2        0.00       81.21
{txt}          1 {c |}{res}     18,651       18.79      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}     99,275      100.00
{txt}
{com}. tab otheruncontlag

{txt}otheruncont {c |}
        lag {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
         -1 {c |}{res}        154        0.15        0.15
{txt}  -.3333333 {c |}{res}          1        0.00        0.16
{txt}          0 {c |}{res}     99,104       99.54       99.69
{txt}        .25 {c |}{res}          1        0.00       99.69
{txt}   .3333333 {c |}{res}          1        0.00       99.70
{txt}         .5 {c |}{res}          7        0.01       99.70
{txt}   .6666667 {c |}{res}          1        0.00       99.70
{txt}          1 {c |}{res}        295        0.30      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}     99,564      100.00
{txt}
{com}. tab pastuncontlag

{txt}pastuncontl {c |}
         ag {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
         -1 {c |}{res}        117        0.12        0.12
{txt}        -.5 {c |}{res}         23        0.02        0.14
{txt}  -.3333333 {c |}{res}          4        0.00        0.14
{txt}       -.25 {c |}{res}          2        0.00        0.15
{txt}  -.1428571 {c |}{res}          1        0.00        0.15
{txt}          0 {c |}{res}     99,280       99.59       99.74
{txt}         .2 {c |}{res}          3        0.00       99.74
{txt}        .25 {c |}{res}          6        0.01       99.75
{txt}   .3333333 {c |}{res}         13        0.01       99.76
{txt}         .5 {c |}{res}         27        0.03       99.79
{txt}          1 {c |}{res}        210        0.21      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}     99,686      100.00
{txt}
{com}. *Those look good.  
. 
. *STATE
. merge m:1 sid using 000_StateCodes
{res}
{txt}{col 5}Result{col 38}# of obs.
{col 5}{hline 41}
{col 5}not matched{col 30}{res}               2
{txt}{col 9}from master{col 30}{res}               0{txt}  (_merge==1)
{col 9}from using{col 30}{res}               2{txt}  (_merge==2)

{col 5}matched{col 30}{res}         140,236{txt}  (_merge==3)
{col 5}{hline 41}

{com}. drop if _merge==2
{txt}(2 observations deleted)

{com}. drop _merge
{txt}
{com}. 
. *IDENTIFICATION
. bysort year sid sen dname dno geopost mmdpost: assert _N==1
{txt}
{com}. *Those identify the data, good.  
. 
. *ORGANIZE
. sort year sid sen dname dno geopost mmdpost etype
{txt}
{com}. 
. order ///
> year ///
> state ///
> sid ///
> sfips ///
> sab ///
> sen ///
> dname ///
> dno ///
> geopost ///
> mmdpost ///
> specpost ///
> dtype ///
> dseats ///
> popnum ///
> redist ///
> redist1 ///
> redist2 ///
> redist3 ///
> regime ///
> nest ///
> nest1 ///
> nest2 ///
> nest3 ///
> etype ///
> deter ///
> eseats ///
> firstcase ///
> seatsup ///
> totalseats ///
> propup ///
> dvote ///
> rvote ///
> ovote ///
> dcand ///
> rcand ///
> ocand ///
> dwin ///
> rwin ///
> owin ///
> dinc ///
> rinc ///
> oinc ///
> dinc2 ///
> rinc2 ///
> oinc2 ///
> dinc3 ///
> rinc3 ///
> oinc3 ///
> dother ///
> rother ///
> oother ///
> dleg2 ///
> rleg2 ///
> oleg2 ///
> dleg3 ///
> rleg3 ///
> oleg3 ///
> dpast ///
> rpast ///
> opast ///
> dswitch ///
> rswitch ///
> dswitchwin ///
> rswitchwin ///
> dstealth ///
> rstealth ///
> dstealthwin ///
> rstealthwin ///
> dontuse ///
> votemiss ///
> bigthird ///
> mixeduncont ///
> partuncont ///
> uncont ///
> dper ///
> cand ///
> inc ///
> inc2 ///
> inc3 ///
> other ///
> leg2 ///
> leg3 ///
> past ///
> switch ///
> switchwin ///
> stealth ///
> stealthwin ///
> maxtermz ///
> mintermz ///
> maxweight ///
> minweight ///
> propuplag ///
> dperlag ///
> dwinlag ///
> dvotelag ///
> rvotelag ///
> ovotelag ///
> candlag ///
> inclag ///
> inc2lag ///
> inc3lag ///
> otherlag ///
> leg2lag ///
> leg3lag ///
> pastlag ///
> switchlag ///
> switchwinlag ///
> stealthlag ///
> stealthwinlag ///
> dontuselag ///
> bigthirdlag ///
> mixeduncontlag ///
> partuncontlag ///
> uncontlag ///
> incuncontlag ///
> otheruncontlag ///
> pastuncontlag ///
> lagyearmin ///
> lagyearmax ///
> minweightlag ///
> maxweightlag
{txt}
{com}. 
. save $mainfile, replace
{txt}file 102slersuoacontest20181024.dta saved

{com}. 
. *UOA=CHAMBERYEAR
. *Create a file that tells the users what proportion of seats are dontuse=1, and what additional proportion of seats are bigthird=1 or 2.  
. clear
{txt}
{com}. use $mainfile
{txt}
{com}. gen seatsdontuse=dontuse*eseats
{txt}
{com}. gen bigthird1seats=eseats if bigthird==1&dontuse==0
{txt}(139,772 missing values generated)

{com}. gen bigthird2seats=eseats if bigthird==2&dontuse==0
{txt}(139,452 missing values generated)

{com}. recode bigthird1seats bigthird2seats (.=0)
{txt}(bigthird1seats: 139772 changes made)
(bigthird2seats: 139452 changes made)

{com}. rename eseats seatsup2
{res}{txt}
{com}. collapse (sum) seatsup2 seatsdontuse bigthird1seats bigthird2seats (mean) seatsup totalseats, by(year state sid sen)
{txt}
{com}. order state sid sen year totalseats seatsup seatsup2 seatsdontuse bigthird1seats bigthird2seats
{txt}
{com}. sort state sen year
{txt}
{com}. export delimited 103uoachamyear$datezzz.csv, replace
{res}{txt}file 103uoachamyear20181024.csv saved

{com}. 
. *SEATCHECK
. *See if all seats in a chamber are accounted for.  
. clear
{txt}
{com}. use $mainfile
{txt}
{com}. egen totalseats2=sum(eseats), by(year sid sen)
{txt}
{com}. gen dif=totalseats-totalseats2
{txt}(1 missing value generated)

{com}. *STATEHOUSES
. tab sid dif if sen==0

 {txt}number of {c |}
  state in {c |}                                dif
alpha list {c |}         0          1         44         45         46         48 {c |}     Total
{hline 11}{c +}{hline 66}{c +}{hline 10}
         1 {c |}{res}     1,366          0          0          0          0          0 {txt}{c |}{res}     1,366 
{txt}         2 {c |}{res}       866          0          0          0          0          0 {txt}{c |}{res}       866 
{txt}         3 {c |}{res}       747          0          0          0          0          0 {txt}{c |}{res}       747 
{txt}         4 {c |}{res}     2,500          0          0          0          0          0 {txt}{c |}{res}     2,500 
{txt}         5 {c |}{res}     2,000          0          0          0          0          0 {txt}{c |}{res}     2,000 
{txt}         6 {c |}{res}     1,625          0          0          0          0          0 {txt}{c |}{res}     1,625 
{txt}         7 {c |}{res}     3,827          0          0          0          0          0 {txt}{c |}{res}     3,827 
{txt}         8 {c |}{res}     1,021          0          0          0          0          0 {txt}{c |}{res}     1,021 
{txt}         9 {c |}{res}     2,878        119          0          0          0          0 {txt}{c |}{res}     2,997 
{txt}        10 {c |}{res}     4,530          0          0          0          0          0 {txt}{c |}{res}     4,530 
{txt}        11 {c |}{res}     1,094          0          0          0          0          0 {txt}{c |}{res}     1,094 
{txt}        12 {c |}{res}     1,680          0          0          0          0          0 {txt}{c |}{res}     1,680 
{txt}        13 {c |}{res}     2,537          0          0          0          0          0 {txt}{c |}{res}     2,537 
{txt}        14 {c |}{res}     2,128          0          0          0          0          0 {txt}{c |}{res}     2,128 
{txt}        15 {c |}{res}     2,524          0          0          0          0          0 {txt}{c |}{res}     2,524 
{txt}        16 {c |}{res}     3,125          0          0          0          0          0 {txt}{c |}{res}     3,125 
{txt}        17 {c |}{res}     2,400          0          0          0          0          0 {txt}{c |}{res}     2,400 
{txt}        19 {c |}{res}     3,600          0          0          0          0          0 {txt}{c |}{res}     3,600 
{txt}        20 {c |}{res}       744          0          0          0          0          0 {txt}{c |}{res}       744 
{txt}        21 {c |}{res}     4,205          0          0          0          0          0 {txt}{c |}{res}     4,205 
{txt}        22 {c |}{res}     2,750          0          0          0          0          0 {txt}{c |}{res}     2,750 
{txt}        23 {c |}{res}     2,814        133          0          0          0          0 {txt}{c |}{res}     2,947 
{txt}        24 {c |}{res}     1,586          0          0          0          0          0 {txt}{c |}{res}     1,586 
{txt}        25 {c |}{res}     4,075          0          0          0          0          0 {txt}{c |}{res}     4,075 
{txt}        26 {c |}{res}     2,299          0          0          0          0          0 {txt}{c |}{res}     2,299 
{txt}        28 {c |}{res}       988          0          0          0          0          0 {txt}{c |}{res}       988 
{txt}        29 {c |}{res}     4,137          0          0          0          0          0 {txt}{c |}{res}     4,137 
{txt}        30 {c |}{res}       964          0          0          0          0          0 {txt}{c |}{res}       964 
{txt}        31 {c |}{res}     1,750          0          0          0          0          0 {txt}{c |}{res}     1,750 
{txt}        32 {c |}{res}     3,750          0          0          0          0          0 {txt}{c |}{res}     3,750 
{txt}        33 {c |}{res}     2,095          0          0          0          0          0 {txt}{c |}{res}     2,095 
{txt}        34 {c |}{res}       752          0         25         51         96         48 {txt}{c |}{res}       972 
{txt}        35 {c |}{res}     2,475          0          0          0          0          0 {txt}{c |}{res}     2,475 
{txt}        36 {c |}{res}     2,521          0          0          0          0          0 {txt}{c |}{res}     2,521 
{txt}        37 {c |}{res}     1,500          0          0          0          0          0 {txt}{c |}{res}     1,500 
{txt}        38 {c |}{res}     5,075          0          0          0          0          0 {txt}{c |}{res}     5,075 
{txt}        39 {c |}{res}     2,300          0          0          0          0          0 {txt}{c |}{res}     2,300 
{txt}        40 {c |}{res}     2,866          0          0          0          0          0 {txt}{c |}{res}     2,866 
{txt}        41 {c |}{res}       859          0          0          0          0          0 {txt}{c |}{res}       859 
{txt}        42 {c |}{res}     2,475          0          0          0          0          0 {txt}{c |}{res}     2,475 
{txt}        43 {c |}{res}     3,750          0          0          0          0          0 {txt}{c |}{res}     3,750 
{txt}        44 {c |}{res}     1,863          0          0          0          0          0 {txt}{c |}{res}     1,863 
{txt}        45 {c |}{res}     1,715          0          0          0          0          0 {txt}{c |}{res}     1,715 
{txt}        46 {c |}{res}     2,168          0          0          0          0          0 {txt}{c |}{res}     2,168 
{txt}        47 {c |}{res}     2,452          0          0          0          0          0 {txt}{c |}{res}     2,452 
{txt}        48 {c |}{res}     1,256          0          0          0          0          0 {txt}{c |}{res}     1,256 
{txt}        49 {c |}{res}     2,477          0          0          0          0          0 {txt}{c |}{res}     2,477 
{txt}        50 {c |}{res}     1,056          0          0          0          0          0 {txt}{c |}{res}     1,056 
{txt}{hline 11}{c +}{hline 66}{c +}{hline 10}
     Total {c |}{res}   110,165        252         25         51         96         48 {txt}{c |}{res}   110,637 
{txt}
{com}. *one missing election in fl and mn, probably those special elections that were one year after the general.  
. *ND is also obviously not correct.  
. tab year sid if dif==1&sen==0&sid!=34

           {txt}{c |}  number of state in
           {c |}      alpha list
      year {c |}         9         23 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
      2014 {c |}{res}       119          0 {txt}{c |}{res}       119 
{txt}      2016 {c |}{res}         0        133 {txt}{c |}{res}       133 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}       119        133 {txt}{c |}{res}       252 
{txt}
{com}. *fl 2014
. *mn 2016
. list dno if sid==9&sen==0&year==2015
{txt}
        {c TLC}{hline 5}{c TRC}
        {c |} {res}dno {txt}{c |}
        {c LT}{hline 5}{c RT}
134362. {c |} {res} 13 {txt}{c |}
        {c BLC}{hline 5}{c BRC}

{com}. *#13 is in 2015.  
. list dno geopost if sid==23&sen==0&year==2016&dno==32
{txt}
        {c TLC}{hline 5}{c -}{hline 9}{c TRC}
        {c |} {res}dno   geopost {txt}{c |}
        {c LT}{hline 5}{c -}{hline 9}{c RT}
137184. {c |} {res} 32         1 {txt}{c |}
        {c BLC}{hline 5}{c -}{hline 9}{c BRC}

{com}. *geopost=1 is there, but geopost=2 isn't.  
. list dno if sid==23&sen==0&year==2017
{txt}
{com}. *STATESENATES
. tab sid dif if sen==1

 {txt}number of {c |}
  state in {c |}                                                                 dif
alpha list {c |}         0          1          2          3          6          7          8          9         10         11         12         13 {c |}     Total
{hline 11}{c +}{hline 132}{c +}{hline 10}
         1 {c |}{res}       455          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       455 
{txt}         2 {c |}{res}        20         19         18         17         25          0          0         33        149          0          0          0 {txt}{c |}{res}       281 
{txt}         3 {c |}{res}       747          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       747 
{txt}         4 {c |}{res}       175          0          0          0          0          0         27          0          0          0          0          0 {txt}{c |}{res}       529 
{txt}         5 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       500 
{txt}         6 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       451 
{txt}         7 {c |}{res}       900          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       900 
{txt}         8 {c |}{res}       124          0          0          0          0          0          0          0        108         90          0          0 {txt}{c |}{res}       322 
{txt}         9 {c |}{res}       288          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       657 
{txt}        10 {c |}{res}     1,400          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}     1,400 
{txt}        11 {c |}{res}        74          0          0          0          0          0          0          0         30         49         91         48 {txt}{c |}{res}       300 
{txt}        12 {c |}{res}       903          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       903 
{txt}        13 {c |}{res}       353          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       897 
{txt}        14 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       616 
{txt}        15 {c |}{res}        50          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       682 
{txt}        16 {c |}{res}       520          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       528 
{txt}        17 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       459 
{txt}        19 {c |}{res}       857          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       857 
{txt}        20 {c |}{res}       547          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       547 
{txt}        21 {c |}{res}     1,000          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}     1,000 
{txt}        22 {c |}{res}       456          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       458 
{txt}        23 {c |}{res}       871          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       875 
{txt}        24 {c |}{res}       676          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       676 
{txt}        25 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       427 
{txt}        26 {c |}{res}        50          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       648 
{txt}        28 {c |}{res}         0          0          0          0          0          0          0         30        162         50          0          0 {txt}{c |}{res}       242 
{txt}        29 {c |}{res}       600          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       600 
{txt}        30 {c |}{res}       541          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       550 
{txt}        31 {c |}{res}       378          0          0          0         36         35         34          0          0          0          0          0 {txt}{c |}{res}       528 
{txt}        32 {c |}{res}     1,523          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}     1,523 
{txt}        33 {c |}{res}       958          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       958 
{txt}        34 {c |}{res}         0          0          0          0          0          0          0          0         39          0          0          0 {txt}{c |}{res}       631 
{txt}        35 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       421 
{txt}        36 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       611 
{txt}        37 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0         51 {txt}{c |}{res}       389 
{txt}        38 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       626 
{txt}        39 {c |}{res}     1,154          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}     1,154 
{txt}        40 {c |}{res}       598          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       601 
{txt}        41 {c |}{res}       821          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       821 
{txt}        42 {c |}{res}        33          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       437 
{txt}        43 {c |}{res}       186          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       480 
{txt}        44 {c |}{res}        29          0          0          0          0          0          0          0          0         18          0         80 {txt}{c |}{res}       389 
{txt}        45 {c |}{res}       208          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       208 
{txt}        46 {c |}{res}       474          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       475 
{txt}        47 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       629 
{txt}        48 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       433 
{txt}        49 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       416 
{txt}        50 {c |}{res}        30          0          0          0          0          0          0          0          0          0         31         12 {txt}{c |}{res}       361 
{txt}{hline 11}{c +}{hline 132}{c +}{hline 10}
     Total {c |}{res}    17,999         19         18         17         61         35         61         63        488        207        122        191 {txt}{c |}{res}    29,598 


 {txt}number of {c |}
  state in {c |}                                                                 dif
alpha list {c |}        14         15         16         17         18         19         20         21         22         23         24         25 {c |}     Total
{hline 11}{c +}{hline 132}{c +}{hline 10}
         1 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       455 
{txt}         2 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       281 
{txt}         3 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       747 
{txt}         4 {c |}{res}         0          0         38        162        119          0          0          0          0          0          0          0 {txt}{c |}{res}       529 
{txt}         5 {c |}{res}         0          0          0          0          0          0        500          0          0          0          0          0 {txt}{c |}{res}       500 
{txt}         6 {c |}{res}         0         20        114        198        119          0          0          0          0          0          0          0 {txt}{c |}{res}       451 
{txt}         7 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       900 
{txt}         8 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       322 
{txt}         9 {c |}{res}         0          0          0         46        110         84        120          0          0          0          0          0 {txt}{c |}{res}       657 
{txt}        10 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}     1,400 
{txt}        11 {c |}{res}         7          0          0          0          0          0          0          0          0          0          1          0 {txt}{c |}{res}       300 
{txt}        12 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       903 
{txt}        13 {c |}{res}         0          0          0          0         41        240         78          0          0          0          0          0 {txt}{c |}{res}       897 
{txt}        14 {c |}{res}         0          0          0          0          0          0          0          0          0          0         44        572 {txt}{c |}{res}       616 
{txt}        15 {c |}{res}         0         35          0          0         32          0          0         29          0         27         26        450 {txt}{c |}{res}       682 
{txt}        16 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       528 
{txt}        17 {c |}{res}         0          0          0         21         20        418          0          0          0          0          0          0 {txt}{c |}{res}       459 
{txt}        19 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       857 
{txt}        20 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       547 
{txt}        21 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}     1,000 
{txt}        22 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       458 
{txt}        23 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       875 
{txt}        24 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       676 
{txt}        25 {c |}{res}         0          0         36        391          0          0          0          0          0          0          0          0 {txt}{c |}{res}       427 
{txt}        26 {c |}{res}         0         20          0          0          0          0          0          0         28         27        156        325 {txt}{c |}{res}       648 
{txt}        28 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       242 
{txt}        29 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       600 
{txt}        30 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       550 
{txt}        31 {c |}{res}         0          0          0          0         24          0          0          0          0          0          0          0 {txt}{c |}{res}       528 
{txt}        32 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}     1,523 
{txt}        33 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       958 
{txt}        34 {c |}{res}         0          0          0          0         35          0          0         51         46         98        163        120 {txt}{c |}{res}       631 
{txt}        35 {c |}{res}         0         72        221        128          0          0          0          0          0          0          0          0 {txt}{c |}{res}       421 
{txt}        36 {c |}{res}         0          0          0          0          0          0          0         54          0        125        432          0 {txt}{c |}{res}       611 
{txt}        37 {c |}{res}       128        210          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       389 
{txt}        38 {c |}{res}         0          0          0          0          0          0          0          0          0          0         26        600 {txt}{c |}{res}       626 
{txt}        39 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}     1,154 
{txt}        40 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       601 
{txt}        41 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       821 
{txt}        42 {c |}{res}        19         54        187        144          0          0          0          0          0          0          0          0 {txt}{c |}{res}       437 
{txt}        43 {c |}{res}         0        144        150          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       480 
{txt}        44 {c |}{res}       178         84          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       389 
{txt}        45 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       208 
{txt}        46 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       475 
{txt}        47 {c |}{res}         0          0          0          0          0          0          0         28          0        234        175        192 {txt}{c |}{res}       629 
{txt}        48 {c |}{res}         0         38         72        323          0          0          0          0          0          0          0          0 {txt}{c |}{res}       433 
{txt}        49 {c |}{res}         0          0        272        144          0          0          0          0          0          0          0          0 {txt}{c |}{res}       416 
{txt}        50 {c |}{res}        55        221         12          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       361 
{txt}{hline 11}{c +}{hline 132}{c +}{hline 10}
     Total {c |}{res}       387        898      1,102      1,557        500        742        698        162         74        511      1,023      2,259 {txt}{c |}{res}    29,598 


 {txt}number of {c |}
  state in {c |}                                                                 dif
alpha list {c |}        26         27         28         33         34         36         37         38         39         40         41         45 {c |}     Total
{hline 11}{c +}{hline 132}{c +}{hline 10}
         1 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       455 
{txt}         2 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       281 
{txt}         3 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       747 
{txt}         4 {c |}{res}         0          8          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       529 
{txt}         5 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       500 
{txt}         6 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       451 
{txt}         7 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       900 
{txt}         8 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       322 
{txt}         9 {c |}{res}         0          0          0          0          0          0          0          0          9          0          0          0 {txt}{c |}{res}       657 
{txt}        10 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}     1,400 
{txt}        11 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       300 
{txt}        12 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       903 
{txt}        13 {c |}{res}         0          0          0          0          0         23          0         42         80         38          0          0 {txt}{c |}{res}       897 
{txt}        14 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       616 
{txt}        15 {c |}{res}         0          0         33          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       682 
{txt}        16 {c |}{res}         0          0          0          0          0          0          0          6          2          0          0          0 {txt}{c |}{res}       528 
{txt}        17 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       459 
{txt}        19 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       857 
{txt}        20 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       547 
{txt}        21 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}     1,000 
{txt}        22 {c |}{res}         0          0          0          0          0          0          2          0          0          0          0          0 {txt}{c |}{res}       458 
{txt}        23 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       875 
{txt}        24 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       676 
{txt}        25 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       427 
{txt}        26 {c |}{res}         0         21         21          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       648 
{txt}        28 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       242 
{txt}        29 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       600 
{txt}        30 {c |}{res}         0          0          0          0          0          0          0          6          3          0          0          0 {txt}{c |}{res}       550 
{txt}        31 {c |}{res}         0          0          0          9          8          0          0          0          0          2          2          0 {txt}{c |}{res}       528 
{txt}        32 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}     1,523 
{txt}        33 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       958 
{txt}        34 {c |}{res}        54         25          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       631 
{txt}        35 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       421 
{txt}        36 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       611 
{txt}        37 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       389 
{txt}        38 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       626 
{txt}        39 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}     1,154 
{txt}        40 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          3 {txt}{c |}{res}       601 
{txt}        41 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       821 
{txt}        42 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       437 
{txt}        43 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       480 
{txt}        44 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       389 
{txt}        45 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       208 
{txt}        46 {c |}{res}         0          0          0          0          0          0          0          0          1          0          0          0 {txt}{c |}{res}       475 
{txt}        47 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       629 
{txt}        48 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       433 
{txt}        49 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       416 
{txt}        50 {c |}{res}         0          0          0          0          0          0          0          0          0          0          0          0 {txt}{c |}{res}       361 
{txt}{hline 11}{c +}{hline 132}{c +}{hline 10}
     Total {c |}{res}        54         54         54          9          8         23          2         54         95         40          2          3 {txt}{c |}{res}    29,598 


 {txt}number of {c |}
  state in {c |}          dif
alpha list {c |}        56         65 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
         1 {c |}{res}         0          0 {txt}{c |}{res}       455 
{txt}         2 {c |}{res}         0          0 {txt}{c |}{res}       281 
{txt}         3 {c |}{res}         0          0 {txt}{c |}{res}       747 
{txt}         4 {c |}{res}         0          0 {txt}{c |}{res}       529 
{txt}         5 {c |}{res}         0          0 {txt}{c |}{res}       500 
{txt}         6 {c |}{res}         0          0 {txt}{c |}{res}       451 
{txt}         7 {c |}{res}         0          0 {txt}{c |}{res}       900 
{txt}         8 {c |}{res}         0          0 {txt}{c |}{res}       322 
{txt}         9 {c |}{res}         0          0 {txt}{c |}{res}       657 
{txt}        10 {c |}{res}         0          0 {txt}{c |}{res}     1,400 
{txt}        11 {c |}{res}         0          0 {txt}{c |}{res}       300 
{txt}        12 {c |}{res}         0          0 {txt}{c |}{res}       903 
{txt}        13 {c |}{res}         2          0 {txt}{c |}{res}       897 
{txt}        14 {c |}{res}         0          0 {txt}{c |}{res}       616 
{txt}        15 {c |}{res}         0          0 {txt}{c |}{res}       682 
{txt}        16 {c |}{res}         0          0 {txt}{c |}{res}       528 
{txt}        17 {c |}{res}         0          0 {txt}{c |}{res}       459 
{txt}        19 {c |}{res}         0          0 {txt}{c |}{res}       857 
{txt}        20 {c |}{res}         0          0 {txt}{c |}{res}       547 
{txt}        21 {c |}{res}         0          0 {txt}{c |}{res}     1,000 
{txt}        22 {c |}{res}         0          0 {txt}{c |}{res}       458 
{txt}        23 {c |}{res}         0          4 {txt}{c |}{res}       875 
{txt}        24 {c |}{res}         0          0 {txt}{c |}{res}       676 
{txt}        25 {c |}{res}         0          0 {txt}{c |}{res}       427 
{txt}        26 {c |}{res}         0          0 {txt}{c |}{res}       648 
{txt}        28 {c |}{res}         0          0 {txt}{c |}{res}       242 
{txt}        29 {c |}{res}         0          0 {txt}{c |}{res}       600 
{txt}        30 {c |}{res}         0          0 {txt}{c |}{res}       550 
{txt}        31 {c |}{res}         0          0 {txt}{c |}{res}       528 
{txt}        32 {c |}{res}         0          0 {txt}{c |}{res}     1,523 
{txt}        33 {c |}{res}         0          0 {txt}{c |}{res}       958 
{txt}        34 {c |}{res}         0          0 {txt}{c |}{res}       631 
{txt}        35 {c |}{res}         0          0 {txt}{c |}{res}       421 
{txt}        36 {c |}{res}         0          0 {txt}{c |}{res}       611 
{txt}        37 {c |}{res}         0          0 {txt}{c |}{res}       389 
{txt}        38 {c |}{res}         0          0 {txt}{c |}{res}       626 
{txt}        39 {c |}{res}         0          0 {txt}{c |}{res}     1,154 
{txt}        40 {c |}{res}         0          0 {txt}{c |}{res}       601 
{txt}        41 {c |}{res}         0          0 {txt}{c |}{res}       821 
{txt}        42 {c |}{res}         0          0 {txt}{c |}{res}       437 
{txt}        43 {c |}{res}         0          0 {txt}{c |}{res}       480 
{txt}        44 {c |}{res}         0          0 {txt}{c |}{res}       389 
{txt}        45 {c |}{res}         0          0 {txt}{c |}{res}       208 
{txt}        46 {c |}{res}         0          0 {txt}{c |}{res}       475 
{txt}        47 {c |}{res}         0          0 {txt}{c |}{res}       629 
{txt}        48 {c |}{res}         0          0 {txt}{c |}{res}       433 
{txt}        49 {c |}{res}         0          0 {txt}{c |}{res}       416 
{txt}        50 {c |}{res}         0          0 {txt}{c |}{res}       361 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}         2          4 {txt}{c |}{res}    29,598 
{txt}
{com}. *Looks good generally.  Of course, with staggered seats, many aren't all up, but the state senates without staggered terms always have the same number of seats up as total seats in the legislature.  
. recode dif (1/max=1), gen(difb)
{txt}(11800 differences between dif and difb)

{com}. tab sid difb if sen==1

 {txt}number of {c |}
  state in {c |}     RECODE of dif
alpha list {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
         1 {c |}{res}       455          0 {txt}{c |}{res}       455 
{txt}         2 {c |}{res}        20        261 {txt}{c |}{res}       281 
{txt}         3 {c |}{res}       747          0 {txt}{c |}{res}       747 
{txt}         4 {c |}{res}       175        354 {txt}{c |}{res}       529 
{txt}         5 {c |}{res}         0        500 {txt}{c |}{res}       500 
{txt}         6 {c |}{res}         0        451 {txt}{c |}{res}       451 
{txt}         7 {c |}{res}       900          0 {txt}{c |}{res}       900 
{txt}         8 {c |}{res}       124        198 {txt}{c |}{res}       322 
{txt}         9 {c |}{res}       288        369 {txt}{c |}{res}       657 
{txt}        10 {c |}{res}     1,400          0 {txt}{c |}{res}     1,400 
{txt}        11 {c |}{res}        74        226 {txt}{c |}{res}       300 
{txt}        12 {c |}{res}       903          0 {txt}{c |}{res}       903 
{txt}        13 {c |}{res}       353        544 {txt}{c |}{res}       897 
{txt}        14 {c |}{res}         0        616 {txt}{c |}{res}       616 
{txt}        15 {c |}{res}        50        632 {txt}{c |}{res}       682 
{txt}        16 {c |}{res}       520          8 {txt}{c |}{res}       528 
{txt}        17 {c |}{res}         0        459 {txt}{c |}{res}       459 
{txt}        19 {c |}{res}       857          0 {txt}{c |}{res}       857 
{txt}        20 {c |}{res}       547          0 {txt}{c |}{res}       547 
{txt}        21 {c |}{res}     1,000          0 {txt}{c |}{res}     1,000 
{txt}        22 {c |}{res}       456          2 {txt}{c |}{res}       458 
{txt}        23 {c |}{res}       871          4 {txt}{c |}{res}       875 
{txt}        24 {c |}{res}       676          0 {txt}{c |}{res}       676 
{txt}        25 {c |}{res}         0        427 {txt}{c |}{res}       427 
{txt}        26 {c |}{res}        50        598 {txt}{c |}{res}       648 
{txt}        28 {c |}{res}         0        242 {txt}{c |}{res}       242 
{txt}        29 {c |}{res}       600          0 {txt}{c |}{res}       600 
{txt}        30 {c |}{res}       541          9 {txt}{c |}{res}       550 
{txt}        31 {c |}{res}       378        150 {txt}{c |}{res}       528 
{txt}        32 {c |}{res}     1,523          0 {txt}{c |}{res}     1,523 
{txt}        33 {c |}{res}       958          0 {txt}{c |}{res}       958 
{txt}        34 {c |}{res}         0        631 {txt}{c |}{res}       631 
{txt}        35 {c |}{res}         0        421 {txt}{c |}{res}       421 
{txt}        36 {c |}{res}         0        611 {txt}{c |}{res}       611 
{txt}        37 {c |}{res}         0        389 {txt}{c |}{res}       389 
{txt}        38 {c |}{res}         0        626 {txt}{c |}{res}       626 
{txt}        39 {c |}{res}     1,154          0 {txt}{c |}{res}     1,154 
{txt}        40 {c |}{res}       598          3 {txt}{c |}{res}       601 
{txt}        41 {c |}{res}       821          0 {txt}{c |}{res}       821 
{txt}        42 {c |}{res}        33        404 {txt}{c |}{res}       437 
{txt}        43 {c |}{res}       186        294 {txt}{c |}{res}       480 
{txt}        44 {c |}{res}        29        360 {txt}{c |}{res}       389 
{txt}        45 {c |}{res}       208          0 {txt}{c |}{res}       208 
{txt}        46 {c |}{res}       474          1 {txt}{c |}{res}       475 
{txt}        47 {c |}{res}         0        629 {txt}{c |}{res}       629 
{txt}        48 {c |}{res}         0        433 {txt}{c |}{res}       433 
{txt}        49 {c |}{res}         0        416 {txt}{c |}{res}       416 
{txt}        50 {c |}{res}        30        331 {txt}{c |}{res}       361 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}    17,999     11,599 {txt}{c |}{res}    29,598 
{txt}
{com}. *The above looks good.  
. gen dif2=seatsup-totalseats2
{txt}(1 missing value generated)

{com}. tab dif2

       {txt}dif2 {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}    139,983       99.82       99.82
{txt}          1 {c |}{res}        252        0.18      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}    140,235      100.00
{txt}
{com}. *only .18% of cases are in a chamber that is off.  They are only off by one.  
. tab sid sen if dif2!=0

 {txt}number of {c |}
  state in {c |}   senate
alpha list {c |}         0 {c |}     Total
{hline 11}{c +}{hline 11}{c +}{hline 10}
         9 {c |}{res}       120 {txt}{c |}{res}       120 
{txt}        23 {c |}{res}       133 {txt}{c |}{res}       133 
{txt}{hline 11}{c +}{hline 11}{c +}{hline 10}
     Total {c |}{res}       253 {txt}{c |}{res}       253 
{txt}
{com}. *It's just the FL and MN houses.
. tab sid year if dif2!=0

 {txt}number of {c |}
  state in {c |}               year
alpha list {c |}      2014       2015       2016 {c |}     Total
{hline 11}{c +}{hline 33}{c +}{hline 10}
         9 {c |}{res}       119          1          0 {txt}{c |}{res}       120 
{txt}        23 {c |}{res}         0          0        133 {txt}{c |}{res}       133 
{txt}{hline 11}{c +}{hline 33}{c +}{hline 10}
     Total {c |}{res}       119          1        133 {txt}{c |}{res}       253 
{txt}
{com}. *2014, 2015 for FL, 2016 for MN, so they're definitely the ones from above.  
. clear
{txt}
{com}. 
. 
{txt}end of do-file

{com}. log close
      {txt}name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\User\Dropbox\04_SLERs\CODE\UOAContest\104Log20181024.smcl
  {txt}log type:  {res}smcl
 {txt}closed on:  {res}24 Oct 2018, 02:36:12
{txt}{.-}
{smcl}
{txt}{sf}{ul off}